[php-src] master: core: Deprecate using `namespace` as class constant name (#22964)

NickSdot via GitHub <[email protected]>
Newsgroups gmane.comp.php.cvs.general
Message-ID <[email protected]>
Author: NickSdot (NickSdot)
Committer: GitHub (web-flow)
Pusher: Girgias
Date: 2026-08-10T21:03:40+01:00

Commit: https://github.com/php/php-src/commit/c383b8cae02338f32d2fa460f003223003df1acb
Raw diff: https://github.com/php/php-src/commit/c383b8cae02338f32d2fa460f003223003df1acb.diff

core: Deprecate using `namespace` as class constant name (#22964)

RFC: https://wiki.php.net/rfc/deprecations_php_8_6#deprecate_using_namespace_as_a_class_constant_name

Changed paths:
  A  Zend/tests/deprecate_namespace_class_constant.phpt
  M  NEWS
  M  UPGRADING
  M  Zend/tests/grammar/semi_reserved_005.phpt
  M  Zend/zend_API.c


Diff:

diff --git a/NEWS b/NEWS
index 4ed01591ea4c..23f9fad2e3f4 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ PHP                                                                        NEWS
     operation depth. (iliaal)
 
 - Core:
+  . Deprecated "namespace" as a class constant name. (NickSdot)
   . Changed run-tests.php to run in parallel by default, using up to 10
     automatically detected workers. Pass -j1 for sequential execution.
     (NickSdot)
diff --git a/UPGRADING b/UPGRADING
index 754157e75b22..424e4189057c 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -441,6 +441,7 @@ PHP 8.6 UPGRADE NOTES
 ========================================
 
 - Core:
+  . Using "namespace" as a class constant name is deprecated.
   . Specifying a return type of array|null / ?array for __debugInfo() is now
     deprecated. Specify array instead.
   . Returning values from __construct() and __destruct() is now deprecated.
diff --git a/Zend/tests/deprecate_namespace_class_constant.phpt b/Zend/tests/deprecate_namespace_class_constant.phpt
new file mode 100644
index 000000000000..c44ebbc258da
--- /dev/null
+++ b/Zend/tests/deprecate_namespace_class_constant.phpt
@@ -0,0 +1,67 @@
+--TEST--
+Using "namespace" as a class constant name is deprecated
+--FILE--
+<?php
+
+define('namespace', 'global constant value');
+
+class Foo {
+    const NAMESPACE = 'class constant value';
+}
+
+interface Bar {
+    const Namespace = 'interface constant value';
+}
+
+enum Baz {
+    const nAmEsPaCe = 'enum constant value';
+}
+
+trait YooTrait {
+    const namespace = 'trait constant value';
+}
+
+class Sup {
+    use YooTrait;
+}
+
+enum Yoo: string {
+    case namespace = 'enum case value';
+}
+
+class Can {
+    public static $namespace = 'property value';
+
+    public static function namespace() {
+        return 'method value';
+    }
+}
+
+echo Foo::NAMESPACE, PHP_EOL;
+echo Bar::Namespace, PHP_EOL;
+echo Baz::nAmEsPaCe, PHP_EOL;
+echo Yoo::namespace->value, PHP_EOL;
+echo Sup::namespace, PHP_EOL;
+echo Can::$namespace, PHP_EOL;
+echo Can::namespace(), PHP_EOL;
+echo constant('namespace'), PHP_EOL;
+
+?>
+--EXPECTF--
+Deprecated: Declaring class constant called 'namespace' is deprecated in %s on line %d
+
+Deprecated: Declaring interface constant called 'namespace' is deprecated in %s on line %d
+
+Deprecated: Declaring enum constant called 'namespace' is deprecated in %s on line %d
+
+Deprecated: Declaring trait constant called 'namespace' is deprecated in %s on line %d
+
+Deprecated: Declaring enum constant called 'namespace' is deprecated in %s on line %d
+class constant value
+interface constant value
+enum constant value
+enum case value
+trait constant value
+property value
+method value
+global constant value
diff --git a/Zend/tests/grammar/semi_reserved_005.phpt b/Zend/tests/grammar/semi_reserved_005.phpt
index 4a9a19e11812..06c428eacbc3 100644
--- a/Zend/tests/grammar/semi_reserved_005.phpt
+++ b/Zend/tests/grammar/semi_reserved_005.phpt
@@ -160,7 +160,8 @@ echo Obj::__NAMESPACE__, PHP_EOL;
 
 echo "\nDone\n";
 ?>
---EXPECT--
+--EXPECTF--
+Deprecated: Declaring class constant called 'namespace' is deprecated in %s on line %d
 empty
 callable
 trait
diff --git a/Zend/zend_API.c b/Zend/zend_API.c
index d7ed141a3e1d..e47c489dcee7 100644
--- a/Zend/zend_API.c
+++ b/Zend/zend_API.c
@@ -4821,6 +4821,9 @@ ZEND_API zend_class_constant *zend_declare_typed_class_constant(zend_class_entry
 	if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_CLASS))) {
 		zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
 				"A class constant must not be called 'class'; it is reserved for class name fetching");
+	} else if (zend_string_equals_literal_ci(name, "namespace")) {
+		zend_error(E_DEPRECATED, "Declaring %s constant called 'namespace' is deprecated",
+			zend_get_object_type(ce));
 	}
 
 	if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.