在 php 5.6 的包含文件中定义常量


Defining constants in include file in php 5.6

这是一个代码,在 PHP<5.6 中很好,但自从在 PHP5.6 中迁移脚本以来,我无法让它正常工作。

我的脚本.php :

define('FOO','hello');
include('somefile.php');
echo ANOTHER_FOO;

某文件.php :

define('FOO','hi');
define('ANOTHER_FOO',FOO.' world');

我的脚本.php打印:

  • 在 PHP<5.6 中:"hello world"
  • 在 PHP5.6 中:"嗨世界"

预期的结果是"你好世界"。如何重新定义FOO?我知道 FOO 不应该在某个文件中重新定义.php但这不是我只是想理解它为什么会发生的真正重点。

谢谢!

编辑:

define('FOO','hello');
define('FOO','hi');
define('ANOTHER_FOO',FOO.' world');
echo ANOTHER_FOO;

这工作正常,问题只在于包含。

编辑:

https://bugs.php.net/bug.php?id=68765

这似乎是opcache和php 5.6的已知错误:

  • PHP 5.6.6 和 opcache 禁用:"hello world"
  • PHP 5.6.6 和 opcache 已启用:"嗨世界"
  • PHP 5.5.22 和 opcache 禁用:"hello world"
  • PHP 5.5.22 和 opcache 已启用:"hello world"

同时添加一个触摸以使缓存无效 做诀窍:

define('FOO','hello');
touch('somefile.php');
include('somefile.php');
echo ANOTHER_FOO;