PHP 常量未定义 - 但它们已定义!
我使用常量在脚本中设置各种配置变量。
INC_PATH 常量在包含类库的脚本中定义。
define('INC_PATH',$_SERVER['DOCUMENT_ROOT'].'includes/');
include('class.lib.php');
类库包含各种 include('someClass.php') 行。它还包含:
require(INC_PATH.'DB.class.php');
类库抛出一个通知:
Use of undefined constant INC_PATH - assumed 'INC_PATH'
类库如何看不到 INC_PATH 常量已被定义?我以为常量是全局的?
I'm using constants to set various configuration variables within a script.
The INC_PATH constant is defined within the script that includes the class library.
define('INC_PATH',$_SERVER['DOCUMENT_ROOT'].'includes/');
include('class.lib.php');
The class library contains various include('someClass.php') lines. It also contains:
require(INC_PATH.'DB.class.php');
The class library throws a notice:
Use of undefined constant INC_PATH - assumed 'INC_PATH'
How is the class library not able to see that the INC_PATH constant has been defined? I thought constants were global?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,但它们必须之前定义:
为了回应您的评论,
我无法重现:
a.php
b.php.inc
c.php.inc
询问
a.php
给出:Yes, but they must be defined before:
In response to your comment
I can't reproduce that:
a.php
b.php.inc
c.php.inc
Asking for
a.php
gives:一个大胆的猜测(不过值得检查):如果您的行在声明
INC_PATH常量之前被调用,则可能会出现此通知。
修改您的类库文件并验证谁实际包含该文件:
A wild guess (worth checking though) : This notice could arise if your line
is called sometime before the declaration of the INC_PATH constant.
Modify your class library file and verify who actually includes the file :
是否有可能调用的
define()
位于未执行的代码块内,例如if()
语句?如果在包含
class.lib.php
之前立即验证INC_PATH
的值,会得到什么?Is it possible that the
define()
called is within a block of code that isn't being executed, like anif()
statement?What do you get if you verify the value of
INC_PATH
immediately before you includeclass.lib.php
?编写代码
可能您在块内
。如果您在任何块中编写代码,如果该块未执行,则该块中声明的变量或常量将不会被初始化。
假设,您编写了这样的代码,
它将给出这样的输出:
更正此问题以解决您的问题。
Probably you write the code
within a block.
If yo write your code within any block, if the block is not executed then the variable or constants declared in the block will not been initialized.
Suppose, You make a code like this,
It will give a output like that:
Correct this to solve your problem.
您确定这正是问题所在吗?在访问不带引号的键的数组值时,通常会显示此错误消息:
错误消息中添加的单引号是指示符,引号不会添加到常量中。
Are you sure this is exactly the problem? This error message is moytly displayed on accessing array values with no quotes for the keys:
The added single quotes in your error message are the indicator, quotes will not be added to constant.