简体   繁体   English

在执行 PHP 脚本的每一行之后,是否可以运行 PHP function?

[英]Is it possible to run a PHP function after each line of a PHP script is executed?

My question is similar to this: Is it possible to run code after each line in Ruby?我的问题与此类似: 是否可以在 Ruby 中的每一行之后运行代码? However I want to do it in PHP.但是我想在 PHP 中执行此操作。

Is it possible and if so, how?有可能吗?如果可以,怎么做?

You can register a tick handler :您可以注册一个刻度处理程序

Ticks蜱虫

A tick is an event that occurs for every N low-level tickable statements executed by the parser within the declare block.滴答是在声明块中由解析器执行的每 N 个低级可滴答语句发生的事件。 The value for N is specified using ticks=N within the declare blocks's directive section. N 的值是在声明块的指令部分中使用 ticks=N 指定的。

Not all statements are tickable.并非所有陈述都是可勾选的。 Typically, condition expressions and argument expressions are not tickable.通常,条件表达式和参数表达式是不可勾选的。

As you can see, it's not exactly as "each line of code" unless you only write one tickable statement each line.如您所见,除非您每行只编写一个可勾选的语句,否则它并不完全是“每一行代码”。 But it's the closest you can get.但这是你能得到的最接近的。

Example:例子:

declare(ticks=1);
register_tick_function(function() {
    echo "tick_handler() called\n";
});

echo 'Line 1', PHP_EOL;
echo 'Line 2', PHP_EOL;
echo strtoupper('Line 3'), PHP_EOL;

will output ( demo ):将 output(演示):

tick_handler() called
Line 1
tick_handler() called
Line 2
tick_handler() called
LINE 3
tick_handler() called

I don't think it is within PHP.我认为它不在 PHP 之内。 You could, however, write a PHP script that took your original script and inserted an extra line after each line of the original.但是,您可以编写一个 PHP 脚本,该脚本采用您的原始脚本并在原始脚本的每一行之后插入一个额外的行。

You could try putting a wrapper around your original PHP, like so:您可以尝试在原始 PHP 周围放置一个包装器,如下所示:

<?PHP
$lines = file('original.php');

foreach ($lines as $line) {
    eval($line);
    your_function();
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM