Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Undefined constant error makes no sense

I truly hope I've missed something simple here, but I'm running into a strange issue using class constants in PHP. I created a simple class called Utils and added two class constants, CRYPT_SALT and LOGIN_PAGE. I referenced those from other files, and they worked. Then I added five more class constants, and they don't work. I get "Fatal error: Undefined class constant '' in /var/www/modx/test.php on line ", where is one of the new constants, and is the line where I try to use it.

Here is the Utils class:

<?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;
    }


}

?>

All the member functions were written and tested before I added the class constants, they are working.

And here is test.php, a simple test page to illustrate the problem:

<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";

?>

The exact error I get from test.php is:

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

I've tried the following to solve this:

-- Renaming the constants, including using lower case names without underscores

-- Changing the order of the declarations.

-- Changing from double to single quotes.

-- Commenting out the declarations for CRYPT_SALT and LOGIN_PAGE

-- Showing this code to my co-workers, who are all clueless

Regardless of anything I try, CRYPT_SALT and LOGIN_PAGE work, none of the other constants work. I'm afraid I'm up against some bug deep in PHP's class system. Or maybe I've just stared at this so long that I'm missing the obvious.

like image 674
mrex Avatar asked Jun 27 '12 22:06

mrex


People also ask

Are PHP constants case sensitive?

Class constants are always case-sensitive. Global constants declared with const are always case-sensitive. It should be noted that this applies only to the shortname of the constant, while namespaces in PHP are always case-insensitive. Constants declared with define() are case-sensitive by default.

What is undefined constant in PHP?

Code Inspection: Undefined constantReports the references to constants that are not found in the project files, configured include paths, or among the PHP predefined constants.

How to define a constant variable in PHP?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.


1 Answers

Yes,

The answer is I am an idiot, as usual. :)

I had a second copy of utils.php, in the main directory of the web. That earlier version (with only CRYPT_SALT and LOGIN_PAGE defined) was the one the autoloader was finding first.

@Paolo Bergantino and @David, you were quite right to suggest making sure I was including the file I thought I was. @hakre, thanks for the get_included_files tip.

like image 60
mrex Avatar answered Oct 08 '22 10:10

mrex