php记录执行过的方法,php – 记录所有执行的行

虽然我同意@gnif,因为一个调试器是最适合的,我仍然会回答你的问题,因为它是可能的(不完美,但可能).

考虑你有以下代码:

sometest.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

$hello = 'world';

所以,sometest.php包含以下文件(debug.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";

});

现在,如果我们执行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函数在函数/方法声明中被调用:

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服务器不支持它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值