Re: define() or $variable for application settings?

From: Date: Thu, 09 Feb 2006 20:12:30 +0000
Subject: Re: define() or $variable for application settings?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-230178@lists.php.net to get a copy of this message
At 07:56 PM 2/8/2006, Matt Arnilo S. Baluyos (Mailing Lists) wrote:
I have a config.inc.php file which basically contains all the configuration info that the applications needs (directory/file locations, database credentials, etc). The information there is set using the define() function. However, I've seen some open-source projects which either use $variables or associative arrays to save the values.
A small but significant factor in deciding whether to use define()d constants is that constants can't be integrated into expressions as neatly as can variables. Variables can be embedded in expressions:
        $sUserName = "Froggy";
        echo "Variable: $sUsername.";
        Result:  Variable: Froggy.
Constants can't be embedded in expressions:
        define("sUserName", "Froggy");
        echo "Constant: sUsername.";
        Result:  Constant: sUsername.
In order to use constants in string expressions it's necessary to use concatenation operators:
        echo "Constant: " . sUsername . ".";
Ditto for heredocs: Variables can be embedded in heredoc expressions:
        $sUserName = "Froggy";
        echo <<< hdExample1
        Variable: $sUserName
hdExample1;
        Result:  Variable: Froggy.
Constants can't be embedded in heredoc expressions:
        define("sUserName", "Froggy");
        echo <<< hdExample2
        Constant: sUserName
hdExample2;
        Result:  Constant: sUsername.
Likewise, functions & classes can't be embedded in expressions without use of concatenation operators, and can't be used in heredocs. Of course I'm not suggesting that our inability to embed non-variables in string & heredoc expressions removes them from the toolbox, it simply informs our choices within a given application. Personally I love using heredocs, functions, and classes and simply work around the embedding limitation. Paul

« previous php.general (#230178) next »