php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #49982 Inclusion of token/syntax keyword in namespace causes Parse error
Submitted: 2009-10-24 01:34 UTC Modified: 2009-11-13 19:42 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: swquinn at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3.0 OS: Windows XP Professional
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: swquinn at gmail dot com
New email:
PHP Version: OS:

 

 [2009-10-24 01:34 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-24 02:00 UTC] johannes@php.net
This is expected.

Didn't check the docs, maybe this can be made clearer.
 [2009-10-24 18:44 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?
 [2009-10-24 18:49 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!
 [2009-10-31 14:15 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.
 [2009-11-13 19:42 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."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 08:01:29 2024 UTC