<?php
declare(ticks=1);
// A function called on each tick event
function tick_handler()
{
echo "tick_handler() called/n";
}
register_tick_function('tick_handler');
$a = 1;
if ($a > 0) {
$a += 2;
print($a);
}
?>
这里的register_tick_function用来注册函数,declare(ticks=1);指定没执行1条低级语句,就执行被注册的函数。
为了看它的底层代码,运行如下命令:
$php -r "var_dump( token_get_all(file_get_contents('test.php')) );"
输出片段如下:
……
[2]=>
array(3) {
[0]=>
int(333) // token 号
[1]=>
string(8) "function" // 字符串
[2]=>
int(3) //第几行
}
……
可以通过以下命令,查看token的名称:
$php -r 'echo token_name(367);' //T_OPEN_TAG