PHP 常量未定义 - 但它们已定义!

发布于 2024-09-08 23:26:15 字数 419 浏览 5 评论 0原文

我使用常量在脚本中设置各种配置变量。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

各空 2024-09-15 23:26:15

是的,但它们必须之前定义:

<?php
echo INC_PATH; //undefined
define('INC_PATH', "foo");
echo INC_PATH; //defined

为了回应您的评论,

我无法重现:

a.php

<?php
define('INC_PATH',$_SERVER['DOCUMENT_ROOT']."/");
include('b.php.inc');

b.php.inc

<h1><?php require(INC_PATH . "c.php.inc"); ?></h1>

c.php.inc

<?php echo INC_PATH; ?>

询问 a.php 给出:

<h1>U:/htdocs/</h1>

Yes, but they must be defined before:

<?php
echo INC_PATH; //undefined
define('INC_PATH', "foo");
echo INC_PATH; //defined

In response to your comment

I can't reproduce that:

a.php

<?php
define('INC_PATH',$_SERVER['DOCUMENT_ROOT']."/");
include('b.php.inc');

b.php.inc

<h1><?php require(INC_PATH . "c.php.inc"); ?></h1>

c.php.inc

<?php echo INC_PATH; ?>

Asking for a.php gives:

<h1>U:/htdocs/</h1>
怪我入戏太深 2024-09-15 23:26:15

一个大胆的猜测(不过值得检查):如果您的行在声明

include('class.lib.php');

INC_PATH常量之前被调用,则可能会出现此通知。

修改您的类库文件并验证谁实际包含该文件:

throw new Exception('err'); //-- Verify the stack trace !
require(INC_PATH.'DB.class.php');

A wild guess (worth checking though) : This notice could arise if your line

include('class.lib.php');

is called sometime before the declaration of the INC_PATH constant.

Modify your class library file and verify who actually includes the file :

throw new Exception('err'); //-- Verify the stack trace !
require(INC_PATH.'DB.class.php');
╰ゝ天使的微笑 2024-09-15 23:26:15

是否有可能调用的 define() 位于未执行的代码块内,例如 if() 语句?

如果在包含 class.lib.php 之前立即验证 INC_PATH 的值,会得到什么?

...
...
var_dump(INC_PATH);exit;
include('class.lib.php');

Is it possible that the define() called is within a block of code that isn't being executed, like an if() statement?

What do you get if you verify the value of INC_PATH immediately before you include class.lib.php?

...
...
var_dump(INC_PATH);exit;
include('class.lib.php');
清醇 2024-09-15 23:26:15

编写代码

define('INC_PATH',$_SERVER['DOCUMENT_ROOT'].'includes/');
include('class.lib.php');

可能您在块内


。如果您在任何块中编写代码,如果该块未执行,则该块中声明的变量或常量将不会被初始化。

假设,您编写了这样的代码,

<?php
  $var = false;
  // This if block will not execute
  if($var) // because if($var) will be executed as if(false)
  {
    define('TEST_CONST', 'This is a test');
  }
  echo TEST_CONST;
?>

它将给出这样的输出:

Notice: Use of undefined constant TEST_CONST - assumed 'TEST_CONST' in C:\www\test2.php on line 7
TEST_CONST

更正此问题以解决您的问题。

Probably you write the code

define('INC_PATH',$_SERVER['DOCUMENT_ROOT'].'includes/');
include('class.lib.php');

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,

<?php
  $var = false;
  // This if block will not execute
  if($var) // because if($var) will be executed as if(false)
  {
    define('TEST_CONST', 'This is a test');
  }
  echo TEST_CONST;
?>

It will give a output like that:

Notice: Use of undefined constant TEST_CONST - assumed 'TEST_CONST' in C:\www\test2.php on line 7
TEST_CONST

Correct this to solve your problem.

稀香 2024-09-15 23:26:15

您确定这正是问题所在吗?在访问不带引号的键的数组值时,通常会显示此错误消息:

$array[value]
// instead of:
$array['value']

错误消息中添加的单引号是指示符,引号不会添加到常量中。

Are you sure this is exactly the problem? This error message is moytly displayed on accessing array values with no quotes for the keys:

$array[value]
// instead of:
$array['value']

The added single quotes in your error message are the indicator, quotes will not be added to constant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文