Jump to content

Constants beginning with $


23.12.2012

Recommended Posts

I was thinking about this for a while, and last night I gave it a try. So if you define a new constant beginning with $ using

define('$CONST', 'Michael Jackson');

and then you check to see if it's defined, the defined() function returns true

if(defined('$CONST')) {   	echo 'Defined!';}// This does output Defined!

But how do I use the constant in my code? Is there a way to do it, or was this a PHP design error?

Link to comment
Share on other sites

I was thinking about this for a while, and last night I gave it a try. So if you define a new constant beginning with $ using and then you check to see if it's defined, the defined() function returns true
I guess my question is...why would you do that?
Link to comment
Share on other sites

I wouldn't, I was just wondering why constants beginning with $ do not generate at least a notice in PHP :)

Link to comment
Share on other sites

I suppose the rules for naming constants make $ a character instead of a sigil. I believe the most important function of the sigil in PHP is to remove the possibility of developers trying to use reserved words as variable identifiers. As countless other languages demonstrate, a sigil is not needed to make the interpreter understand what you mean. It is also quite possible that the $ sigil is left over from the days when the only CGI scripting language was Perl. A leftover.In other words, I think the question is, why have the sigil in the first place, and what harm could come from using the sigil character at the start of a constant? A good programming language should have fewer restrictions, and only when they are needed, not more restrictions.

Link to comment
Share on other sites

In other words, I think the question is, why have the sigil in the first place, and what harm could come from using the sigil character at the start of a constant?
1) I think the '$' would come under the heading of syntactical sugar, something generally meant to help humans recognize that it's not just another word, text, or keyword. Lots of languages don't use any special character to denote a variable, but personally I like it.2) As someone else pointed out, it breaks convention. Constants in PHP aren't supposed to have the properties of a variable, so to me it would seem like a assignment. It may do no harm, but to me it just seems wrong, kind of like putting ketchup on your ice cream. If I'm reading someone else's code and I see "$MAX_PRESSURE", the '$' is going to tell me (right or wrong) that it's a variable and not a constant. If you didn't take the time to find the declaration, there wouldn't be any real way of knowing if it was or not (especially if it was being misused, i.e., "$MAX_PRESSURE = (5 * $some_value)."
Link to comment
Share on other sites

This throws an error. You can define the constant with the $, but you can't reference it.

<?phpdefine ('$CONST','donuts');if (defined('$CONST'))        echo 'defined(\'$CONST\'): '.$CONST.PHP_EOL;define ('CONSTANT','sprinkles');if (defined('CONSTANT'))        echo 'defined(\'CONSTANT\'): '.CONSTANT.PHP_EOL;

Output

PHP Notice: Undefined variable: CONST in /var/www/html/test/php/defined.php on line 5defined('$CONST'): defined('CONSTANT'): sprinkles
http://www.php.net/manual/en/language.constants.syntax.php
Link to comment
Share on other sites

As mentioned in the first reply, you can still get to the constant value using the constant() function, it still isn't a good idea though for the reason that the previous example demonstrates.The PHP grammar defines tokens starting with a $ to be T_VARIABLE, and because of that it is misleading to have a constant with a name starting with $, and the behaviour of the PHP interpreter does not allow it to act the same way as a normal constant - as it would require some significant changes to the interpreter to make this behaviour consistent.As it stands you can do:

<?phpdefine('$foo', 'foo');$foo = 'bar'; // allowed, creates a separate variable unrelated to the constantecho $foo; // returns 'bar'echo constant('$foo'); // returns 'foo'?>

This is not ambiguous, because $foo in the assignment and the first echo are only allowed to be T_VARIABLE tokens, and as such the defined constant named '$foo' is not considered. If you changed it to allow '$foo' as a constant to be treated like normal constants, then you would have to add in checks to make sure the '$foo' constant is not declared in the same scope as a $foo variable, and would have to add extra logic into the parser to determine whether the $foo found in echo is a variable token or a constant token, or if neither exist.So... I would consider it more likely that PHP would remove the capability to name constants containing $ characters than change the grammar to allow them completely. In any case, I can't think of any real reason why you would want to do it. Deirdre's Dad mentions that this is possible - and if you were to do that it would make more sense to remove $ as a character with special meaning in the grammar as it loses its usefulness. But considering how much stuff it would break, and the unnecessary complication it adds to the parser, I don't think it's sensible.As to whether it's a PHP design error, I would say: no. Any change to make it disallowed would be purely cosmetic. The way the grammar works means that allowing it is harmless really, it's something that would have to be intentionally misused by a programmer to cause any issues.EDIT: Oh, and one reason why it isn't syntactical sugar really in my opinion: take the example:

<?phpfunction bar() { echo 'foobar'; }$var = 'bar';echo $var(); // outputs 'foobar'?>

That kind of syntax is possible because of the way PHP handles variables, if you didn't have a $ denoting variables, that would not be a valid way of calling the 'bar' function, though I admit that's a fringe use-case.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...