脚本宝典收集整理的这篇文章主要介绍了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",$val ); return $val; } } ?>
在添加类常量之前,所有成员函数都已编写和测试,它们正在工作.
<h1>Test.PHP</h1> <?PHP // Set up autoloader spl_autoload_@R_406_313@( '.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"; ?>
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类课程中的一些错误.或者也许我只是盯着这么久,以至于我错过了这个显而易见的事实.
答案是我像往常一样是个白痴.
以上是脚本宝典为你收集整理的PHP未定义的常量错误没有意义全部内容,希望文章能够帮你解决PHP未定义的常量错误没有意义所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。