PHP命名空间常量未定义


PHP namespace constant undefined

我对PHP比较陌生,我正在尝试在名称空间内对一些代码进行单元测试。不幸的是,配置文件中的常量定义给了我问题。以下是相关代码。

目录结构为:

APITest
config.php
-/logging
-/test/logging

config.php:

<?php
namespace APITest;
define('BASE_DIR', pathinfo(__FILE__, PATHINFO_DIRNAME));
APITest/test/logging/TestLogItem代码:
namespace APITest'test'logging;
require_once( BASE_DIR . '/logging/LogItem.php');   
class TestLogItem extends PHPUnit_Framework_TestCase {
public function testLogItemCreationSuccess(){
    code
}
public function testLogItemCreationException(){
    code
}
public function testGetLogEntry(){
    code
}

错误信息是:PHP Notice: Use of undefined constant BASE_DIR - assumed 'BASE_DIR' in /var/test/APITest/test/logging/TestLogItem.php on line 6 PHP Warning: require_once(BASE_DIR/logging/LogItem.php): failed to open stream: No such file or directory in /var/test/APITest/test/logging/TestLogItem.php on line 6 PHP Fatal error: require_once(): Failed opening required 'BASE_DIR/logging/LogItem.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/test/APITest/test/logging/TestLogItem.php on line 6

您需要在文件中包含config.php

它看起来不像你在任何地方包括文件,所以就PHP而言,常量是没有定义的。