Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

namespace Is Not Valid As A Class Constant Name

namespace is a PHP keyword, and it is allowed inside class for naming methods or properties. After PHP 8.6, it is not possible to use it for constant name.

The actual motivation is a future feature: reserving namespace would allow a future ::namespace pseudo-constant, analogous to ::class, for directory namespaces. For example, replacing stringy APIs like Order\Domain\Entities with \Order\Domain\Entities::namespace, which gives you IDE support, refactoring, and static analysis for free.

PHP code

<?php

class x { const namespace= 1;}

echo x::namespace;

?>

Before

1

After

PHP Deprecated:  Declaring class constant called 'namespace' is deprecated in /codes/namespaceClassConst.php on line 3

Deprecated: Declaring class constant called 'namespace' is deprecated in /codes/namespaceClassConst.php on line 3
1

PHP version change

This behavior was deprecated in 8.6.

This behavior changed in 8.6.

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat