【发布时间】:2012-06-27 22:59:00
【问题描述】:
我真的希望我在这里遗漏了一些简单的东西,但是我在 PHP 中使用类常量时遇到了一个奇怪的问题。我创建了一个名为 Utils 的简单类,并添加了两个类常量 CRYPT_SALT 和 LOGIN_PAGE。我从其他文件中引用了这些,并且它们起作用了。然后我又添加了五个类常量,但它们不起作用。我得到“致命错误:未定义的类常量'' in /var/www/modx/test.php on line”,这是新常量之一,也是我尝试使用它的行。
这里是 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 类系统深处的一些错误。或者也许我只是盯着这个看太久了,以至于我错过了明显的东西。
【问题讨论】:
-
我把这一切都放在一个文件中,没有遇到任何错误——它工作得很好。在不同的服务器上尝试,看看你得到了什么。我无法重现的用例的唯一部分是文件
logger.php -
我不想问,但是您确定 test.php 文件实际上包含您正在处理的同一 utils.php 文件,而不是包含两个常量的原始副本吗?上帝知道我以前做过一些愚蠢的事情,我的头撞到了墙上……你的代码应该可以工作。
-
如果您将
echo Utils::SKEY_LOGGER;放在该类定义的正下方会怎样? -
我从字面上复制并粘贴了您的代码,删除了记录器包含和引用(因为我没有这样的类)并且它工作得很好,按预期输出所有常量?您使用的是什么版本的 PHP,以及;确保您包含正确的 Utils.php 文件并正在修改正确的版本。我花了几个小时调试才发现我更改了错误的文件。
-
它works for me 和我的一个想法并没有破坏它。我建议您将
echo添加到包含类定义的文件中,以验证您确定包含正确的文件。
标签: php class constants undefined