Class Constant Named namespace is Deprecated in PHP 8.6

Class Constant Named namespace is Deprecated in PHP 8.6

Since PHP 8.6, declaring a class constant named namespace is deprecated. Constants with this name in interfaces, enums and traits are deprecated as well. The namespace word is a semi-reserved keyword, and the name is kept free for a possible future ::namespace pseudo-constant, which would work like ::class.

The following class declares a constant with this name:

Config.php

<?php

class Config
{
    public const string NAMESPACE = 'app';
}
<?php

require_once 'Config.php';

echo Config::NAMESPACE;

Output:

Deprecated: Declaring class constant called 'namespace' is deprecated in Config.php on line 5
app

The deprecation is raised where the constant is declared, not where it is read.

The check is case-insensitive, because keywords in PHP are case-insensitive. The namespace, NAMESPACE and Namespace names are all deprecated, and the message always shows the lowercase form. Names which only contain this word, such as MY_NAMESPACE, are not affected. Only constants are affected. Methods and properties named namespace, including static ones, are not deprecated.

To fix the deprecation, the constant must be renamed, for example to NAMESPACE_NAME:

Config.php

<?php

class Config
{
    public const string NAMESPACE_NAME = 'app';
}
<?php

require_once 'Config.php';

echo Config::NAMESPACE_NAME;

Leave a Comment

Cancel reply

Your email address will not be published.