问题描述
概述
在 PHP 5.6 中,似乎添加 declare(ticks=1)
然后使用 register_tick_function()
将遵循任何包含并相应地提供分析信息.
In PHP 5.6 it seems that adding declare(ticks=1)
and then using register_tick_function()
would follow any includes and provide profiling information accordingly.
在 PHP 7+ 中,现在看来我必须在每个文件中添加 declare(ticks=1)
.我用它来分析页面加载时的每个方法调用,现在不想将它添加到我系统中的每个 PHP 文件中(有些我不能,如果它们在库中).
In PHP 7+ however it now seems I have to add declare(ticks=1)
in every file. I use this for profiling every method call on a page load and don't now want to add this to each PHP file in my system (some I can't if they are in libraries).
我在文档中找不到关于对此所做更改的任何内容.
I can't find anything in the docs about changes that were made to this.
复制步骤
创建以下 2 个文件:
Create the below 2 files:
index.php
<?php
declare(ticks=1);
$count = 0;
register_tick_function('ticker');
function ticker() {
global $count;
$count++;
}
$foo = 'foo';
$bar = 'bar';
include dirname(__FILE__) . '/inc.php';
echo $count;
inc.php
<?php
#declare(ticks=1);
$baz = "baz";
$qux = "qux";
结果
在终端中运行 php index.php
给我:
Running php index.php
in the terminal gives me:
- PHP 5.6 - 7
- PHP 7.0 - 5
在 inc.php
中取消注释 declare(ticks=1)
的结果是:
With declare(ticks=1)
uncommented in inc.php
the results are:
- PHP 5.6 - 8
- PHP 7.0 - 8
问题
有没有办法强制它遵循包含并在某种意义上使其在 PHP 7+ 中成为全局的?
Is there any way to way to force it to follow includes and in a sense make it global in PHP 7+?
推荐答案
根据 https://bugs.php.net/bug.php?id=71448
由于一个实现错误,在 PHP 7.0 之前,declare(ticks=1) 指令泄漏到不同的编译单元中.这不是每个文件或每个范围的 declare() 指令应该如何工作.
所以实际上这是一个错误,它曾经像在 PHP 5.6 中那样工作,并且在 PHP 7.0 中添加了正确的实现.不幸的是,这意味着它永远不会奏效,但至少有一个解释.
So in fact it was a bug that it ever did work as it did in PHP 5.6 and the correct implementation has been added in PHP 7.0. Unfortunately this means it will never work but at least there is an explanation.
以下问题的答案显示了如何在 PHP 7+ 中实现这一点
There is an answer on the below question that shows how to achieve this in PHP 7+
这篇关于如何避免在 PHP 7 中重新声明每个文件的刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!