繁体   English   中英

“使用未定义的常量”注意事项,但应定义常量

[英]“Use of undefined constant” notice, but the constant should be defined

有三个文件:common.php,controller.php和user.php。

文件common.php看起来像:

<?php
define("MAXIMUM_NAME_LENGTH", 50);
?>

文件controller.php看起来像:

<?php
include("common.php");
include("user.php");
$user = new user();
?>

文件user.php看起来像:

<?php
class user{
private $var = MAXIMUM_NAME_LENGTH;
}
?>

执行脚本时会发出通知:注意:使用未定义的常量MAXIMUM_NAME_LENGTH - 在xxx行的/.../controller.php中假定为“MAXIMUM_NAME_LENGTH”。 我想在common.php中共享其他文件之间的定义值。 如何以适当的方式做到这一点?

通常,您将使用将要使用它们的文件包含的所有定义。 IE:在您的示例中,使类文件具有$class->setMaxNameLength(); 方法并通过那里传递名称长度定义,这样它不会给你一个错误。

放置error_reporting(E_ALL); include("common.php");之前的行include("common.php");

最有可能你会看到类似的东西:

Warning: include(common.php) [function.include]: failed to open stream: No such file or directory in xxx

表示您的三个文件不在同一文件夹中,或者在include_path不在的路径中,或者它们的组合。

编辑:

如果您使用的是Suhosin ,请检查您的suhosin.executor.include suhosin.executor.include.*设置。

暂无
暂无

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

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