草庐IT

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

coder 2024-04-06 原文

我真的希望我在这里错过了一些简单的东西,但是我在 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/

有关PHP 未定义的常量错误没有意义的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby-on-rails - 未初始化的常量 Psych::Syck (NameError) - 2

    在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到ruby​​gems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决

  4. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  7. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  10. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

随机推荐