PHP 未定义的常量错误没有意义

标签 php class constants undefined

我真的希望我在这里错过了一些简单的东西,但是我在 PHP 中使用类常量时遇到了一个奇怪的问题。我创建了一个名为 Utils 的简单类,并添加了两个类常量,CRYPT_SALT 和 LOGIN_PAGE。我从其他文件中引用了那些,它们起作用了。然后我又添加了五个类常量,但它们不起作用。我收到“ fatal error :在线/var/www/modx/test.php 中未定义的类常量”,新常量之一在哪里,是我尝试使用它的那一行。

这是 Utils 类:

<?php
// 
// Utils.php
//  
// This class is a collection of static utility functions.  Since the methods are static, they should
// all be invoked with:
//
//  Utils::methodName();
//
// This class also contains global constants, which are *not* kept in Config.  They should be accessed with:
//
//  Utils::CONSTANT;
// 
// addToCSVString -- adds an incoming string to a CSV string, possibly prepending a comma and space.  Returns
// addToJSONString -- adds an incoming key/value pair to a JSON string
// jsonify -- takes in a string and replaces control characters and quotes with properly
//

require_once( "logger.php" );

class Utils {

        // Constants 

    const CRYPT_SALT    = '$6$';
    const LOGIN_PAGE    = '/modx/';

        // Session keys

    const SKEY_DEBUG    = 'debug';
    const SKEY_LOGIN    = 'login';
    const SKEY_LANG     = 'curLang';
    const SKEY_UID      = 'userID';
    const SKEY_LOGGER   = 'logger';


        // Members

    public static $debug    = false;

        // Methods

    //
    // addToCSVString -- adds an incoming string to a CSV string, possibly prepending a comma and space.  Returns
    // the new string
    //
    public static function addToCSVString( $csvString, $newVal ) {
        if ( strlen( $csvString ) > 0 ) {
            $csvString  .= ", ";
        }

        return $csvString . $newVal;
    }


    //
    // addToJSONString -- adds an incoming key/value pair to a JSON string
    //
    public static function addToJSONString( $jsonString, $key, $val ) {
        $debug      = self::$debug;

        if ( $debug ) {
            $logger = Logger::singleton();
            $logger->log( "In Utils::addToJSONString" );
            $logger->log( "\$key = [$key]", 1 );
            $logger->log( "\$val = [$val]", 1 );
        }

        if ( strpos( $val, "{" ) === false ) {

            if ( $debug ) {
                $logger->log( "Utils: this is a plain value", 1 );
            }
                // Val is a string

            $val    = self::jsonify( $val );

            return self::addToCSVString( $jsonString, "\"" . $key . "\" : \"" . $val . "\"" );
        } else {
            if ( $debug ) {
                $logger->log( "this is a JSON object", 1 );
            }

                // Val is a JSON object

            return self::addToCSVString( $jsonString, "\"" . $key . "\" : " . $val . "" );
        }
    }


    //
    // jsonify -- takes in a string and replaces control characters and quotes with properly
    // escaped JSON values
    //
    public static function jsonify( $val ) {
        $val    = str_replace( '\\', '\\\\', $val );        // convert backslashes first 
        $val    = str_replace( "\n", '\\n', $val );
        $val    = str_replace( "\r", '\\r', $val );
        $val    = str_replace( "\t", '\\t', $val );
        $val    = str_replace( "\v", '\\v', $val );
        $val    = str_replace( "\f", '\\f', $val );
        $val    = str_replace( "\n", '\\n', $val );
        $val    = str_replace( "\n", '\\n', $val );

        return $val;
    }


}

?>

所有的成员函数都是在我添加类常量之前编写和测试的,它们可以正常工作。

这里是 test.php,一个用来说明问题的简单测试页面:

<h1>Test.php</h1>

<?php

    // Set up autoloader

spl_autoload_extensions( '.php,.inc' );
spl_autoload_register();

    // Test class constants

echo "<b>Testing Utils class constants</b></br>\n"; 
echo 'Utils::CRYPT_SALT = [' . Utils::CRYPT_SALT . "]<br>\n";
echo 'Utils::LOGIN_PAGE = [' . Utils::LOGIN_PAGE . "]<br>\n";
echo 'Utils::SKEY_LOGGER = [' . Utils::SKEY_LOGGER . "]<br>\n";
echo 'Utils::SKEY_DEBUG = [' . Utils::SKEY_DEBUG . "]<br>\n";
echo 'Utils::SKEY_LOGIN = [' . Utils::SKEY_LOGIN . "]<br>\n";
echo 'Utils::SKEY_LANG = [' . Utils::SKEY_LANG . "]<br>\n";
echo 'Utils::SKEY_UID = [' . Utils::SKEY_UID . "]<br>\n";
echo "</br>\n";

?>

我从 test.php 得到的确切错误是:

Fatal error: Undefined class constant 'SKEY_LOGGER' in /var/www/modx/test.php on line 15

我尝试了以下方法来解决这个问题:

-- 重命名常量,包括使用不带下划线的小写名称

-- 改变声明的顺序。

-- 从双引号变为单引号。

-- 注释掉 CRYPT_SALT 和 LOGIN_PAGE 的声明

-- 向我的同事展示这段代码,他们都一无所知

无论我尝试什么,CRYPT_SALT 和 LOGIN_PAGE 都有效,其他常量均无效。恐怕我遇到了 PHP 类系统中的一些错误。或者,也许我只是盯着它看了太久,以至于我错过了显而易见的东西。

最佳答案

是的,

和往常一样,答案是我是个白痴。 :)

我在网站的主目录中有 utils.php 的第二个副本。早期版本(仅定义了 CRYPT_SALT 和 LOGIN_PAGE)是自动加载器首先找到的版本。

@Paolo Bergantino 和@David,您建议确保我包含我认为包含的文件是非常正确的。 @hakre,感谢 get_included_files 提示。

关于PHP 未定义的常量错误没有意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11235940/

相关文章:

ios - 如何在两个类之间进行交集

c++ - 为 const global 跳过 extern 仍然可以正常工作

php - 来自 Web 服务器的响应主体的值为 "<body>&lt;script language="javaScript">window.parent.document.cookie ="Authorization=;path=/";

php - php资源是通过引用传递的吗?

php - 如何使用 PHP 在输入标签中上传具有两个不同名称的图像?并调整它们的大小

java - 二元运算符的操作数类型错误 '=='

php - 注意: undefined offset :在第641行的/my/Zend/ib/Search/Lucene/Index/SegmentInfo.php中

c++ - 返回在非类型模板化类中定义的 typedef

c++ - 从 const double 的指针返回指向 double 的指针

c++ - 来自函数签名的 const 函数签名