Reputation: 251
If we are dealing with a large codebase where the global variable may be referenced in many different files:
ctags
to jump to declaration takes you to the local statement where the global is brought into scope in the current fileglobal $foo
using grep
or ack
results in a list of files where the global was brought into scope, but not the exact declaration of the global variableIs there any tool/vim plugin/etc that will tell you exactly where the global is being declared for the first time?
Update: I guess you could set a watch breakpoint and look for the first access to the variable. Unfortunately, this type of breakpoint is not yet supported in Xdebug, so for my particular setup, it wouldn't work.
Upvotes: 4
Views: 2297
Reputation: 754
There doesn't need to be any declaration beyond the global $foo
declarations you are finding. These do declare the global variable. It can easily be the case that the global variable is only referenced from within these functions.
The following code works, for example:
function print_it() {
global $my_global;
echo $my_global;
}
function set_it() {
global $my_global;
$my_global = 17;
}
set_it();
print_it(); // echos 17
Upvotes: 0
Reputation: 11824
Are you a Unix-based OS?
I usually do something like grep -rn 'global $myvar' .
in the top-level directory. This does a recursive search of files in the current directory for the declaration.
Or, if you want to get fancy you can search for only PHP files:
grep -rn --include '*.php' 'global $myvar' .
Upvotes: 3