|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74257 Leading slash in namespace declaration
Submitted: 2017-03-16 12:47 UTC Modified: 2017-03-16 16:46 UTC
From: david at grudl dot com Assigned:
Status: Not a bug Package: Compile Failure
PHP Version: 7.1.2 OS:
Private report: No CVE-ID: None
 [2017-03-16 12:47 UTC] david at grudl dot com
Leading slash in namespace declaration can cause silent ignorance of namespace declaration.

Test script:

define('X', 'Y');

namespace \X;

class Test

echo Test::class;

Expected result:
Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-16 16:46 UTC]
This is not a bug, but rather a feature in namespaces known as relative names.

In short, the following line:
namespace \X;

is not a namespace *declaration*, but rather an *expression* that resolves to the symbol X of the current namespace. The "namespace \" part (where space is optional) is either evaluated to the currently defined namespace (it is simply ignored for the global namespace).

When utilising this feature, symbols that are not found in the current namespace will not fallback to the globally defined functions or constants.

So, the following works:

But the following will error (due to an undefined PHP_ZTS constant in the current namespace):
namespace test;

Whilst obviously the following will work as expected (by falling back to the globally defined PHP_ZTS constant):
namespace test;

See the manual for further information:
 [2017-03-16 16:46 UTC]
-Status: Open +Status: Not a bug
 [2017-03-16 16:46 UTC]
^ See above comment...
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC