Page 1 of 1
Find where variable is defined
Posted: Mon Feb 18, 2008 9:40 am
by coolbung
Hi all,
is it possible in PHP to find out where a variable is defined.
I.e. find out the filename and line number where a variable is set.
To be more specific i'm working on an installation of Joomla which has been worked upon by some developer and it seems he has reset the database name somewhere is his code. viz., the $mosConfig_db variable
Re: Find where variable is defined
Posted: Mon Feb 18, 2008 10:13 am
by John Cartwright
A little bored this morning
Code: Select all
$needle = '$foobar =';
$content = file_get_contents('path/to/file');
preg_match_all('#.*?'. preg_quote($needle) .'#is', $content, $matches);
if (isset($matches[0][0])) {
'Needle found on Line '. count(explode(PHP_EOL, $matches[0][0]));
} else {
'Needle not found';
}
Re: Find where variable is defined
Posted: Mon Feb 18, 2008 10:18 am
by coolbung
Thats my problem,
I dont know where the variable is being defined.
I want to know the filename as well as line number where the variable gets defined.
The filename atleast.
Re: Find where variable is defined
Posted: Mon Feb 18, 2008 10:21 am
by John Cartwright
use glob() to search the directory, or search these boards for recursive directory iterator (if you need to search subdir's as well).
You are better off using grep though if you do not need do this programmatically
Re: Find where variable is defined
Posted: Mon Feb 18, 2008 11:23 am
by coolbung
Thanks for the lead, but i could not find the syntax to make grep scan recursively
I'm using this syntax -
grep -o '$mosConfig_db' *
Re: Find where variable is defined
Posted: Mon Feb 18, 2008 11:29 am
by John Cartwright
Re: Find where variable is defined
Posted: Mon Feb 18, 2008 11:40 am
by coolbung
Thanks JCart. I was able to write the syntax.
Just for reference i used - grep -ro '$varname' *
Re: Find where variable is defined
Posted: Mon Feb 18, 2008 11:41 am
by coolbung
Btw u can mark this closed from my side!