我使用常量在脚本中设置各种配置变量.
在包含类库的脚本中定义了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常量?我以为常量是全局的?
解决方法:
是的,但是必须在以下条件之前定义它们:
<?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>