【问题标题】:PHP namespace constant undefinedPHP 命名空间常量未定义
【发布时间】:2013-06-12 20:05:50
【问题描述】:

我对 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

【问题讨论】:

    标签: php namespaces constants


    【解决方案1】:

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

    您似乎没有在任何地方包含该文件,因此就 PHP 而言,未定义常量。

    【讨论】:

      猜你喜欢
      • 2013-06-03
      • 2013-03-20
      • 2023-01-25
      • 2011-12-18
      • 2016-03-29
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      相关资源
      最近更新 更多