php – 记录所有执行的行

前端之家收集整理的这篇文章主要介绍了php – 记录所有执行的行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有一个简单的方法(每一行之后都没有粘贴fwrites)让 PHP将每条执行行写入日志?
虽然我同意@gnif,因为一个调试器是最适合的,我仍然会回答你的问题,因为它是可能的(不完美,但可能).

考虑你有以下代码

sometest.PHP

<?PHP

declare(ticks=1);

include_once 'debug.PHP';

$a = 'foo';
$b = 'bar';
$c = $a . $b;
$d = $e = "hello";
strlen($d);

include 'somefile.PHP';

somefile.PHP

<?PHP

$hello = 'world';

所以,sometest.PHP包含以下文件(debug.PHP):

<?PHP

register_tick_function(function(){
    $backtrace = debug_backtrace();
    $line = $backtrace[0]['line'] - 1;
    $file = $backtrace[0]['file'];

    if ($file == __FILE__) return;

    static $fp,$cur,$buf;
    if (!isset($fp[$file])) {
        $fp[$file] = fopen($file,'r');
        $cur[$file] = 0;
    }

    if (isset($buf[$file][$line])) {
        $code = $buf[$file][$line];
    } else {
        do {
            $code = fgets($fp[$file]);
            $buf[$file][$cur[$file]] = $code;
        } while (++$cur[$file] <= $line);
    }

    $line++;
    echo "$code called in $file on line $line\n";
});

它是registers a tick function,也是declares the tick interval.它将跟踪使用回溯调用文件/行.

现在,如果我们执行sometest.PHP,我们将得到:

include_once 'debug.PHP';
 called in sometest.PHP on line 5
$a = 'foo';
 called in sometest.PHP on line 7
$b = 'bar';
 called in sometest.PHP on line 8
$c = $a . $b;
 called in sometest.PHP on line 9
$d = $e = "hello";
 called in sometest.PHP on line 10
strlen($d);
 called in sometest.PHP on line 11
$hello = 'world';
 called in somefile.PHP on line 3
include 'somefile.PHP';
 called in sometest.PHP on line 13

你可以看到somefile.PHP包含在最后,尽管它在$hello =’world’之前被调用.这是因为当包含完成该行,而不是启动时,tick函数将被调用.

此外,tick函数函数/方法声明中被调用

<?PHP

function foo() {
    return 'bar';
}

foo();

会给你一些像:

}
 called in somefunc.PHP on line 5   # this is the function declaration
foo();
 called in somefunc.PHP on line 7   # this is the function call

注意:使用ticks时要小心,因为在5.3.0之前,线程Web服务器不支持它.

原文链接:https://www.f2er.com/php/133186.html

猜你在找的PHP相关文章