Skip to content

Commit 0b4fbf2

Browse files
authored
ext/reflection: Add ReflectionConstant::inNamespace() method (#20902)
1 parent 297b253 commit 0b4fbf2

File tree

8 files changed

+76
-5
lines changed

8 files changed

+76
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ PHP NEWS
8686
- Reflection:
8787
. Fixed bug GH-20217 (ReflectionClass::isIterable() incorrectly returns true
8888
for classes with property hooks). (alexandre-daubois)
89+
. Added ReflectionConstant::inNamespace(). (Khaled Alam)
8990

9091
- Session:
9192
. Fixed bug 71162 (updateTimestamp never called when session data is empty).

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ PHP 8.6 UPGRADE NOTES
115115
6. New Functions
116116
========================================
117117

118+
- Reflection:
119+
. ReflectionConstant::inNamespace()
120+
118121
- Standard:
119122
. `clamp()` returns the given value if in range, else return the nearest
120123
bound.

ext/reflection/php_reflection.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7714,6 +7714,20 @@ ZEND_METHOD(ReflectionConstant, getName)
77147714
RETURN_STR_COPY(const_->name);
77157715
}
77167716

7717+
ZEND_METHOD(ReflectionConstant, inNamespace)
7718+
{
7719+
reflection_object *intern;
7720+
zend_constant *const_;
7721+
7722+
ZEND_PARSE_PARAMETERS_NONE();
7723+
7724+
GET_REFLECTION_OBJECT_PTR(const_);
7725+
7726+
const char *backslash = zend_memrchr(ZSTR_VAL(const_->name), '\\', ZSTR_LEN(const_->name));
7727+
RETURN_BOOL(backslash);
7728+
}
7729+
/* }}} */
7730+
77177731
ZEND_METHOD(ReflectionConstant, getNamespaceName)
77187732
{
77197733
reflection_object *intern;

ext/reflection/php_reflection.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,8 @@ public function __construct(string $name) {}
915915

916916
public function getName(): string {}
917917

918+
public function inNamespace(): bool {}
919+
918920
public function getNamespaceName(): string {}
919921

920922
public function getShortName(): string {}

ext/reflection/php_reflection_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/reflection/php_reflection_decl.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
ReflectionConstant::inNamespace()
3+
--FILE--
4+
<?php
5+
6+
namespace Foo\Bar {
7+
const NAMESPACED_CONST = 'in namespace';
8+
}
9+
10+
namespace {
11+
const GLOBAL_CONST = 'global';
12+
13+
$rc1 = new ReflectionConstant('GLOBAL_CONST');
14+
var_dump($rc1->inNamespace());
15+
var_dump($rc1->getNamespaceName());
16+
var_dump($rc1->getShortName());
17+
18+
$rc2 = new ReflectionConstant('Foo\Bar\NAMESPACED_CONST');
19+
var_dump($rc2->inNamespace());
20+
var_dump($rc2->getNamespaceName());
21+
var_dump($rc2->getShortName());
22+
23+
$rc3 = new ReflectionConstant('E_ERROR');
24+
var_dump($rc3->inNamespace());
25+
var_dump($rc3->getNamespaceName());
26+
var_dump($rc3->getShortName());
27+
}
28+
29+
?>
30+
--EXPECT--
31+
bool(false)
32+
string(0) ""
33+
string(12) "GLOBAL_CONST"
34+
bool(true)
35+
string(7) "Foo\Bar"
36+
string(16) "NAMESPACED_CONST"
37+
bool(false)
38+
string(0) ""
39+
string(7) "E_ERROR"

ext/reflection/tests/ReflectionConstant_ns.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ namespace {
1414
var_dump(new \ReflectionConstant('\\C'));
1515
var_dump(new \ReflectionConstant('Foo\\C'));
1616
var_dump(new \ReflectionConstant('\\Foo\\C'));
17+
var_dump((new \ReflectionConstant('C'))->inNamespace());
18+
var_dump((new \ReflectionConstant('\\C'))->inNamespace());
19+
var_dump((new \ReflectionConstant('Foo\\C'))->inNamespace());
20+
var_dump((new \ReflectionConstant('\\Foo\\C'))->inNamespace());
1721
var_dump((new \ReflectionConstant('C'))->getNamespaceName());
1822
var_dump((new \ReflectionConstant('\\C'))->getNamespaceName());
1923
var_dump((new \ReflectionConstant('Foo\\C'))->getNamespaceName());
@@ -42,6 +46,10 @@ object(ReflectionConstant)#1 (1) {
4246
["name"]=>
4347
string(6) "\Foo\C"
4448
}
49+
bool(false)
50+
bool(false)
51+
bool(true)
52+
bool(true)
4553
string(0) ""
4654
string(0) ""
4755
string(3) "Foo"

0 commit comments

Comments
 (0)