1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

ext/reflection: Add ReflectionConstant::inNamespace() method (#20902)

This commit is contained in:
Khaled Alam
2026-02-17 07:28:32 +02:00
committed by GitHub
parent 297b25316b
commit 0b4fbf2e70
8 changed files with 76 additions and 5 deletions

1
NEWS
View File

@@ -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).

View File

@@ -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.

View File

@@ -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;

View File

@@ -915,6 +915,8 @@ class ReflectionConstant implements Reflector
public function getName(): string {}
public function inNamespace(): bool {}
public function getNamespaceName(): string {}
public function getShortName(): string {}

View File

@@ -1,5 +1,5 @@
/* This is a generated file, edit php_reflection.stub.php instead.
* Stub hash: dba3ec692c7c90d59d67f6e5323dc31997fc92e0
* Stub hash: b09497083efa7035dab6047f6d845ceaec81579e
* Has decl header: yes */
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1, IS_ARRAY, 0)
@@ -703,6 +703,8 @@ ZEND_END_ARG_INFO()
#define arginfo_class_ReflectionConstant_getName arginfo_class_ReflectionFunction___toString
#define arginfo_class_ReflectionConstant_inNamespace arginfo_class_ReflectionFunctionAbstract_hasTentativeReturnType
#define arginfo_class_ReflectionConstant_getNamespaceName arginfo_class_ReflectionFunction___toString
#define arginfo_class_ReflectionConstant_getShortName arginfo_class_ReflectionFunction___toString
@@ -985,6 +987,7 @@ ZEND_METHOD(ReflectionFiber, getCallable);
ZEND_METHOD(ReflectionFiber, getTrace);
ZEND_METHOD(ReflectionConstant, __construct);
ZEND_METHOD(ReflectionConstant, getName);
ZEND_METHOD(ReflectionConstant, inNamespace);
ZEND_METHOD(ReflectionConstant, getNamespaceName);
ZEND_METHOD(ReflectionConstant, getShortName);
ZEND_METHOD(ReflectionConstant, getValue);
@@ -1355,6 +1358,7 @@ static const zend_function_entry class_ReflectionFiber_methods[] = {
static const zend_function_entry class_ReflectionConstant_methods[] = {
ZEND_ME(ReflectionConstant, __construct, arginfo_class_ReflectionConstant___construct, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionConstant, getName, arginfo_class_ReflectionConstant_getName, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionConstant, inNamespace, arginfo_class_ReflectionConstant_inNamespace, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionConstant, getNamespaceName, arginfo_class_ReflectionConstant_getNamespaceName, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionConstant, getShortName, arginfo_class_ReflectionConstant_getShortName, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionConstant, getValue, arginfo_class_ReflectionConstant_getValue, ZEND_ACC_PUBLIC)

View File

@@ -1,12 +1,12 @@
/* This is a generated file, edit php_reflection.stub.php instead.
* Stub hash: dba3ec692c7c90d59d67f6e5323dc31997fc92e0 */
* Stub hash: b09497083efa7035dab6047f6d845ceaec81579e */
#ifndef ZEND_PHP_REFLECTION_DECL_dba3ec692c7c90d59d67f6e5323dc31997fc92e0_H
#define ZEND_PHP_REFLECTION_DECL_dba3ec692c7c90d59d67f6e5323dc31997fc92e0_H
#ifndef ZEND_PHP_REFLECTION_DECL_b09497083efa7035dab6047f6d845ceaec81579e_H
#define ZEND_PHP_REFLECTION_DECL_b09497083efa7035dab6047f6d845ceaec81579e_H
typedef enum zend_enum_PropertyHookType {
ZEND_ENUM_PropertyHookType_Get = 1,
ZEND_ENUM_PropertyHookType_Set = 2,
} zend_enum_PropertyHookType;
#endif /* ZEND_PHP_REFLECTION_DECL_dba3ec692c7c90d59d67f6e5323dc31997fc92e0_H */
#endif /* ZEND_PHP_REFLECTION_DECL_b09497083efa7035dab6047f6d845ceaec81579e_H */

View File

@@ -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"

View File

@@ -14,6 +14,10 @@ namespace {
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 @@ object(ReflectionConstant)#1 (1) {
["name"]=>
string(6) "\Foo\C"
}
bool(false)
bool(false)
bool(true)
bool(true)
string(0) ""
string(0) ""
string(3) "Foo"