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

I am currently working on a very large project and am under great pressure to finish it soon, and I have a serious problem. The programmer who wrote these last defined variables in a very strange way: configuration variables are not all in one file, they are distributed throughout the project from more than 500 files and 100k + lines of code, and I have a hell of figuring out where a particular variable is, therefore I can fix the problem.
Is there a way to track this variable? I believe that it uses SMARTY (which I can not stand because of such problems), and the variable is a template variable. I'm pretty sure that the variable I'm looking for was originally defined as a PHP variable, then this variable is passed to SMARTY, so I would like to track PHP, but if that is not possible, how can I track down where it defined the variable for SMARTY?

Postscript I am in Vista and do not have access to the ssh server, so "grep" is out of the question.

+7
variables php definition smarty
source share
6 answers

This is one of the reasons why I install Cygwin on all my Windows machines.

grep myvariablename `find project_dir -name "*.php"` 

I cannot imagine programming without grep working.

+5
source share

Brute force method, because sometimes flexible variables are not directly assigned, but their names can be stored in variables that are combined from many lines or are the result of certain functions, which makes it impossible to search files simply by searching / greping.

First, write your own function to print a readable reverse line, i.e.

 function print_backtrace() { $backtrace = debug_backtrace(FALSE); foreach($backtrace as $trace) echo "{$trace['file']} :: {$trace['line']}<br>"; } 

Open the smarty main file ( Smarty.class.php by default), and around line 580 there is a function named assign . Modify it to view the name of the required variable:

 function assign($tpl_var, $value = null) { if($tpl_var == 'FOOBAR') /* Searching for FOOBAR */ { print_backtrace(); exit; } 

The same modification may be required for the second function - assign_by_ref . Now after running the script you should have this output:

 D:\www\test_proj\libs\smarty\Smarty.class.php :: 584 D:\www\test_proj\classes.php :: 11 D:\www\test_proj\classes.php :: 6 D:\www\test_proj\functions.php :: 7 D:\www\test_proj\index.php :: 100 

The second line indicates where the variable was assigned.

+7
source share

This is not an ideal solution, but I find the ransack agent useful for finding large directories and files. I can help you narrow it down. The search results let you read the exact line in which it finds a match in the results pane.

+2
source share

If you are using netbeans editor , just "right click" -> "go to definition"

Or ctrl + click for a variable.

If the editor cannot figure it out, you can return to the "Find Files" option.

+2
source share

Just use one of the available PHP IDEs (or a simple text editor such as Notepad ++ if you are really desperate) and find the variable name in all the source files (most PHP IDEs also support searching where / vars functions were and allows you to go to the corresponding code snippet). Although it seems strange that you don’t know which part of the code calls the template (whether Smarty or something else does not matter). You should be able to deploy code starting with a URI (using any IDE that supports debugging), because this way you must see where the specified variable is defined.

+1
source share

There is an interesting additional option, ugly as hell, but useful if you are truly lost.

If you want to know where THE_NAME was defined, write lines like these in the place where you are sure it starts first:

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

If, later, PHP runs the definition you are looking for, it will write this message:

 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 SomeConfiguration.php file on line 89.

For this to work, you should consider

  • if the framework has HTTP forwards on the path to the code installed in
  • if there are additional commands setting the PHP error message mode

So sometimes it helps to add a few exit('here') so as not to blur the output. Maybe you need to narrow it down a bit or you need to set error_reporting earlier, but you will find it.

+1
source share

All Articles