PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #49982 Inclusion of token/syntax keyword in namespace causes Parse error
Submitted:24 Oct 1:34am UTC Modified: 13 Nov 7:42pm UTC
From:swquinn at gmail dot com Assigned to:
Status:Bogus Category:Documentation problem
Version:5.3.0 OS:Windows XP Professional
Votes:1 Avg. Score:3.0 ± 0.0 Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%) Same OS:1 (100.0%)
View/Vote Developer Edit Submission

[24 Oct 1:34am UTC] swquinn at gmail dot com
Description:
------------
Using a PHP language token, or syntactic keyword within a namespace
causes a Parse error to be thrown by PHP. There is no documentation
anywhere (that I could find) indicating whether keywords are permitted
as part of a namespace name.

If they are not, it would be nice if this is documented.

If they should be, then this is clearly a defect.

It would be _nice_ a keyword could contain any words (language syntax or
otherwise).

Reproduce code:
---------------
<?php
namespace my\default\ns;

class Foo {
  public function bar() {
    return 0;
  }
}

$foo = new Foo();
echo $foo->bar();
?>

Expected result:
----------------
0

Actual result:
--------------
Parse error: syntax error, unexpected T_DEFAULT, expecting T_STRING in
D:\PHP\tests\test.php on line 2
[24 Oct 2:00am UTC] johannes@php.net
This is expected.

Didn't check the docs, maybe this can be made clearer.
[24 Oct 6:44pm UTC] swquinn at gmail dot com
Is there any possibility in the future (e.g. PHP 6+) that the acceptable
vocabulary for namespaces could be expanded so that the use of a keyword
in the namespace doesn't throw a parse error?
[24 Oct 6:49pm UTC] swquinn at gmail dot com
Follow-up: Sorry for the double post. I went back and reviewed the
documentation. Still couldn't find anything, but on the manual page
"Defining Namespaces"
(http://www.php.net/manual/en/language.namespaces.definition.php) there
was a user comment that I missed when going through the documentation
that mentions this issue as well.

If its not in the actual manual, it would be great to note this some
place!

And, thank you for your response!
[31 Oct 2:15pm UTC] kalle@php.net
Namespace names are the same as class names, you cannot use a reserved
keyword as a name anywhere. I think don't we should say on every single
language construct page that you may not use a reserved keyword in a
definition as it makes perfectly sense already.

Adding such things in future things wouldn't be something I would count
on, as it creates many complications since we now have to do extra
checks when we hit a string like:
$default = new Default;

It can also create very unreadable code if you mix language keywords
with API names, and should just be avoided.
[13 Nov 7:42pm UTC] vrana@php.net
Already documented at http://www.php.net/manual/en/reserved.php: "None
of the identifiers listed here should be used as identifiers in any of
your scripts."

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC