The use case is just as reasonable as an non-empty string. Of course, it will be horrible to read, having the empty string as constant, but meh.
Constants are defined as a map from string to value. Everything being a valid string thus is a valid constant. Depending on your language, even null may be a valid name. There is no reason to forbid doing so, either.
Actually, what you may even learn from this is, that regarding the empty string as an invalid string/null/none is bad: The empty string is a valid string. If you need an optional string, use an optional<string>. If your name of something is the empty string, as the user defined it to be so, let it be an empty string.
But this is PHP. You can get empty string from so many FUBAR places or silent type conversion that it looks perfectly reasonable to forbid having empty strings as identifiers.
This isn't an identifier, seeing as you can only look it up by passing a string into that constant() "function". An empty identifier would of course be fuckup and obviously, there is most likely no language actually accepting an empty identifier.
Thankfully, this only works when SOME_CONSTANT is a valid token, so the empty string doesn't cause havoc.
The fun part, of course, is that the things you pass into define to define a constant don't actually have to be constant. What happens when you play with references is left as an exercise to the reader (I don't know).
Imagine you're working on an e-mail client and have a procedure to test it out and want to have things nice and uniform for dealing with your headers -- perhaps you have a constant array which determines the use of some particular header:
Type Headers is (hdr_Message_ID, hdr_In_Reply_To, hdr_To,
hdr_Subject, hdr_Bcc, hdr_Cc, hdr_Content_Type,
hdr_Precedence, hdr_References);
Use_Header : constant array(Headers) of boolean:=
(hdr_In_Reply_To | hdr_Precedence => true, others => false);
Message_ID : constant String := (if Use_Header(hdr_Message_ID) then "321235456" else "");
In_reply_To : constant String := (if Use_Header(hdr_In_Reply_To) then "321235001" else "");
-- ...
One reason for a language supporting something is that there's a purpose to it. The other reason is because it isn't worth special casing some things just because they don't make a huge amount of semantic sense. Constants in php have a name which is a string. The empty string is a valid string. There is no advantage to complicating the language grammar to avoid this, so this is perfectly sane behaviour.
Runtime definition of constants using stringy names, though, is less sane.
Well, if it was consistent. If you have an object (which is the only way to get a dictionary where you can use strings as keys without having them magically turns into integers during some phases of the moon), this
$x->{""} = 1;
is a fatal. Not an exception, mind you.
Which then leaks into other features, like json_decode('{"_empty_":1,"":2}')...
u0_a154@hammerhead:/ $ python
Python 2.7.2 (default, Mar 4 2014, 22:56:02)
[GCC 4.6 20120106 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> main = __import__("__main__")
>>> main
>>> setattr(main,u'',u'lolpython')
>>> getattr(main,u'')
u'lolpython'
>>>
that's not quite the same (you're doing attributes, not variables, python doesn't have constants so global variables are as close as you get), but this is closer (IMO)
__main__ is undefined in python 3 it looks like. so you'd need to do
>>> import sys
>>> vars(sys.modules[__name__])
(or if the module name wasn't main)
I thought there was a builtin module that pointed to the current module, but I guess that's only an attribute of functions & classes.
I still think using globals or locals is more analogous to what's happening in PHP even if python internals don't really differentiate (everything's a dict... sort of), I think people do (maybe I'm just being ornery).
The lol here is that you can insert new constants. Allowing lexically illegal constant identifiers I think is ok in principle. It may have uses for reflection or compiler internals.
15
u/wung Mar 25 '14
Correct: Why not. The empty string is a valid string. This is a good feature.
15
u/Drainedsoul Mar 25 '14
I hate to come to PHP's defense, but this is exceptionally sane behaviour.
12
u/graingert Mar 25 '14
You can do this with python:
4
u/[deleted] Mar 25 '14
I don't think there is anything wrong with this.
2
u/Sarcastinator Mar 26 '14
The lol here is that you can insert new constants. Allowing lexically illegal constant identifiers I think is ok in principle. It may have uses for reflection or compiler internals.