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

后端 未结 6 858
忘掉有多难
忘掉有多难 2020-12-17 22:05

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 defin

6条回答
  •  忘掉有多难
    2020-12-17 22:16

    Brute force way, because sometimes smarty variables are not directly assigned, but their names can be stored in variables, concatenated from many strings or be result of some functions, that makes it impossible to find in files by simply searching / greping.

    Firstly, write your own function to print readable backtrace, ie:

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

    Open main smarty file (Smarty.class.php by default) and around line 580 there is function called assign. Modify it to watch for desired variable name:

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

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

    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
    

    Second line points to the place where variable was first assigned.

提交回复
热议问题