ここで簡単なことを見逃したことを心から願っていますが、PHPでクラス定数を使用すると奇妙な問題が発生します。Utilsという単純なクラスを作成し、CRYPT_SALTとLOGIN_PAGEの2つのクラス定数を追加しました。他のファイルからそれらを参照しましたが、機能しました。次に、さらに5つのクラス定数を追加しましたが、機能しません。/var/www/modx/test.phpの行"に"致命的なエラー:未定義のクラス定数''が表示されます。これは新しい定数の1つであり、これを使用しようとしている行です。
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のクラスシステムの奥深くにあるバグに直面しているのではないかと思います。あるいは、私はこれをずっと見つめていたので、明らかなことを見逃しているのかもしれません。