Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find where a variable is defined in PHP (And/or SMARTY)?

I'm currently working on a very large project, and am under a lot of pressure to finish it soon, and I'm having a serious problem. The programmer who wrote this last defined variables in a very odd way - the config variables aren't all in the same file, they're spread out across the entire project of over 500 files and 100k+ lines of code, and I'm having a hell of a time figuring out where a certain variable is, so I can fix an issue.
Is there a way to track this variable down? I believe he's using SMARTY (Which I can not stand, due to issues like this), and the variable is a template variable. I'm fairly sure that the variable I'm looking for was initially defined as a PHP variable, then that variable is passed into SMARTY, so I'd like to track down the PHP one, however if that's impossible - how can I track down where he defined the variable for SMARTY?

P.S. I'm in Vista, and don't have ssh access to the server, so 'grep' is out of the question.

like image 460
Jon Avatar asked Jun 04 '10 00:06

Jon


1 Answers

There is an interesting further option, ugly like hell but helpful if you are really lost.

If you would like to know where THE_NAME was defined, write lines like these on a place you are sure is run first:

error_reporting(E_ALL);
define('THE_NAME', 'Chuck Norris');

If later PHP will run the definition you are looking for, it will write a notice like this:

Notice: Constant THE_NAME already defined 
in /home/there/can-rip-a-page-out-of-facebook.com/SomeConfiguration.php on line 89 

Then you know that the definition you are looking for is in the file SomeConfiguration.php on line 89.

To have this working, you must consider

  • if there are HTTP forwards in the framework on the way to the code you set in
  • if there are further commands setting the PHP error reporting mode

So sometimes it helps to add some exit('here') in order not to blur the output. Maybe you have to narrow down a bit or you have to set error_reporting earlier, but you'll find it.

like image 119
peter_the_oak Avatar answered Oct 21 '22 19:10

peter_the_oak