假设我这样定义一个变量:
<?php
define("page", "actual-page");
?>
现在我必须将此内容从actual-page
更改为second-page
,我如何实现这一点?
我尝试了以下方法:
<?php
// method 1
page = "second-page";
// method 2
define("page", "second-page");
// method 3
unset(page);
define("page", "second-page");
?>
现在我的想法出去了…我还能做什么?
当你使用PHP的define()函数时,你不是在定义一个变量,而是在定义一个常量。一旦脚本开始执行,常量的值就不能被修改。
可以,使用runkit PECL扩展:
runkit_constant_remove('page');
http://php.net/runkit_constant_remove
http://github.com/zenovich/runkit
sudo pecl install https://github.com/downloads/zenovich/runkit/runkit-1.0.3.tgz
更新:这个模块似乎会引起各种其他事情的麻烦,例如会话系统。
可能是一个很晚的回答,但问题是"现在我的想法出去了…我还能做什么?"
下面是你可以做的"else" -使用$GLOBALS:
<?php
// method 1
$GLOBALS['page'] = 'first-page';
// method 2
$GLOBALS['page'] = "second-page";
// method 3
$GLOBALS['page'] = 'third-page';
?>
我希望它有帮助。我使用它当我做导入,我希望特定的事件不被触发,如果导入标志是在例如:)
define有第三个参数(布尔值),它将第一个定义的常量覆盖为第二个定义的常量。将第一个定义的常量设置为true.
<?php
define("page", "actual-page", true);
// Returns page = actual-page
define("page", "second-page");
// Returns page = second-page
?>