Commit 0b4fbf2e701 for php.net
commit 0b4fbf2e70183d54c42e8d91d390ce54a8e3040e
Author: Khaled Alam <khaledalam.net@gmail.com>
Date: Tue Feb 17 07:28:32 2026 +0200
ext/reflection: Add ReflectionConstant::inNamespace() method (#20902)
diff --git a/NEWS b/NEWS
index fb7e5cf760b..3ea44074336 100644
--- a/NEWS
+++ b/NEWS
@@ -86,6 +86,7 @@ PHP NEWS
- Reflection:
. Fixed bug GH-20217 (ReflectionClass::isIterable() incorrectly returns true
for classes with property hooks). (alexandre-daubois)
+ . Added ReflectionConstant::inNamespace(). (Khaled Alam)
- Session:
. Fixed bug 71162 (updateTimestamp never called when session data is empty).
diff --git a/UPGRADING b/UPGRADING
index 2bf6d495338..3df40b98a33 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -115,6 +115,9 @@ PHP 8.6 UPGRADE NOTES
6. New Functions
========================================
+- Reflection:
+ . ReflectionConstant::inNamespace()
+
- Standard:
. `clamp()` returns the given value if in range, else return the nearest
bound.
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c
index 256468e39a4..232c1e6b36a 100644
--- a/ext/reflection/php_reflection.c
+++ b/ext/reflection/php_reflection.c
@@ -7714,6 +7714,20 @@ ZEND_METHOD(ReflectionConstant, getName)
RETURN_STR_COPY(const_->name);
}
+ZEND_METHOD(ReflectionConstant, inNamespace)
+{
+ reflection_object *intern;
+ zend_constant *const_;
+
+ ZEND_PARSE_PARAMETERS_NONE();
+
+ GET_REFLECTION_OBJECT_PTR(const_);
+
+ const char *backslash = zend_memrchr(ZSTR_VAL(const_->name), '\\', ZSTR_LEN(const_->name));
+ RETURN_BOOL(backslash);
+}
+/* }}} */
+
ZEND_METHOD(ReflectionConstant, getNamespaceName)
{
reflection_object *intern;
diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php
index 147e2f18c9e..8f2c49460b4 100644
--- a/ext/reflection/php_reflection.stub.php
+++ b/ext/reflection/php_reflection.stub.php
@@ -915,6 +915,8 @@ public function __construct(string $name) {}
public function getName(): string {}
+ public function inNamespace(): bool {}
+
public function getNamespaceName(): string {}
public function getShortName(): string {}
diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h
index 5d452451001..e16ea82b0e9 100644
Binary files a/ext/reflection/php_reflection_arginfo.h and b/ext/reflection/php_reflection_arginfo.h differ
diff --git a/ext/reflection/php_reflection_decl.h b/ext/reflection/php_reflection_decl.h
index 7a458bccd1e..f93c1d0c887 100644
Binary files a/ext/reflection/php_reflection_decl.h and b/ext/reflection/php_reflection_decl.h differ
diff --git a/ext/reflection/tests/ReflectionConstant_inNamespace.phpt b/ext/reflection/tests/ReflectionConstant_inNamespace.phpt
new file mode 100644
index 00000000000..16001b5f931
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_inNamespace.phpt
@@ -0,0 +1,39 @@
+--TEST--
+ReflectionConstant::inNamespace()
+--FILE--
+<?php
+
+namespace Foo\Bar {
+ const NAMESPACED_CONST = 'in namespace';
+}
+
+namespace {
+ const GLOBAL_CONST = 'global';
+
+ $rc1 = new ReflectionConstant('GLOBAL_CONST');
+ var_dump($rc1->inNamespace());
+ var_dump($rc1->getNamespaceName());
+ var_dump($rc1->getShortName());
+
+ $rc2 = new ReflectionConstant('Foo\Bar\NAMESPACED_CONST');
+ var_dump($rc2->inNamespace());
+ var_dump($rc2->getNamespaceName());
+ var_dump($rc2->getShortName());
+
+ $rc3 = new ReflectionConstant('E_ERROR');
+ var_dump($rc3->inNamespace());
+ var_dump($rc3->getNamespaceName());
+ var_dump($rc3->getShortName());
+}
+
+?>
+--EXPECT--
+bool(false)
+string(0) ""
+string(12) "GLOBAL_CONST"
+bool(true)
+string(7) "Foo\Bar"
+string(16) "NAMESPACED_CONST"
+bool(false)
+string(0) ""
+string(7) "E_ERROR"
diff --git a/ext/reflection/tests/ReflectionConstant_ns.phpt b/ext/reflection/tests/ReflectionConstant_ns.phpt
index b047fc25dcf..36bf8a08672 100644
--- a/ext/reflection/tests/ReflectionConstant_ns.phpt
+++ b/ext/reflection/tests/ReflectionConstant_ns.phpt
@@ -14,6 +14,10 @@
var_dump(new \ReflectionConstant('\\C'));
var_dump(new \ReflectionConstant('Foo\\C'));
var_dump(new \ReflectionConstant('\\Foo\\C'));
+ var_dump((new \ReflectionConstant('C'))->inNamespace());
+ var_dump((new \ReflectionConstant('\\C'))->inNamespace());
+ var_dump((new \ReflectionConstant('Foo\\C'))->inNamespace());
+ var_dump((new \ReflectionConstant('\\Foo\\C'))->inNamespace());
var_dump((new \ReflectionConstant('C'))->getNamespaceName());
var_dump((new \ReflectionConstant('\\C'))->getNamespaceName());
var_dump((new \ReflectionConstant('Foo\\C'))->getNamespaceName());
@@ -42,6 +46,10 @@
["name"]=>
string(6) "\Foo\C"
}
+bool(false)
+bool(false)
+bool(true)
+bool(true)
string(0) ""
string(0) ""
string(3) "Foo"