Skip to content

Commit 99abdcd

Browse files
committed
update reflection
1 parent 3d46bc2 commit 99abdcd

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

ext/reflection/php_reflection.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,56 @@ static void add_class_vars(zend_class_entry *ce, bool statics, zval *return_valu
40754075
}
40764076
/* }}} */
40774077

4078+
/* {{{ Returns whether the current class is an inner class */
4079+
ZEND_METHOD(ReflectionClass, isInnerClass)
4080+
{
4081+
reflection_object *intern;
4082+
zend_class_entry *ce;
4083+
4084+
ZEND_PARSE_PARAMETERS_NONE();
4085+
4086+
GET_REFLECTION_OBJECT_PTR(ce);
4087+
4088+
// If the class is an inner class, it will have a T_INNER_REF in its name
4089+
// todo: make this better?
4090+
4091+
RETURN_BOOL(strstr(ZSTR_VAL(ce->name), ":>") != NULL);
4092+
}
4093+
/* }}} */
4094+
4095+
/* {{{ Returns true if the class is private */
4096+
ZEND_METHOD(ReflectionClass, isPrivate)
4097+
{
4098+
reflection_object *intern;
4099+
zend_class_entry *ce;
4100+
4101+
ZEND_PARSE_PARAMETERS_NONE();
4102+
GET_REFLECTION_OBJECT_PTR(ce);
4103+
RETURN_BOOL(ce->required_scope && ce->required_scope_absolute);
4104+
}
4105+
4106+
/* {{{ Returns true if the class is protected */
4107+
ZEND_METHOD(ReflectionClass, isProtected)
4108+
{
4109+
reflection_object *intern;
4110+
zend_class_entry *ce;
4111+
4112+
ZEND_PARSE_PARAMETERS_NONE();
4113+
GET_REFLECTION_OBJECT_PTR(ce);
4114+
RETURN_BOOL(ce->required_scope && !ce->required_scope_absolute);
4115+
}
4116+
4117+
/* {{{ Returns true if the class is public */
4118+
ZEND_METHOD(ReflectionClass, isPublic)
4119+
{
4120+
reflection_object *intern;
4121+
zend_class_entry *ce;
4122+
4123+
ZEND_PARSE_PARAMETERS_NONE();
4124+
GET_REFLECTION_OBJECT_PTR(ce);
4125+
RETURN_BOOL(!ce->required_scope);
4126+
}
4127+
40784128
/* {{{ Returns an associative array containing all static property values of the class */
40794129
ZEND_METHOD(ReflectionClass, getStaticProperties)
40804130
{

ext/reflection/php_reflection.stub.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,14 @@ public function getNamespaceName(): string {}
432432
public function getShortName(): string {}
433433

434434
public function getAttributes(?string $name = null, int $flags = 0): array {}
435+
436+
public function isInnerClass(): bool {}
437+
438+
public function isPrivate(): bool {}
439+
440+
public function isProtected(): bool {}
441+
442+
public function isPublic(): bool {}
435443
}
436444

437445
class ReflectionObject extends ReflectionClass

ext/reflection/php_reflection_arginfo.h

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
reflection on inner classes
3+
--FILE--
4+
<?php
5+
6+
namespace n\s;
7+
8+
class Outer {
9+
class Middle {
10+
class Inner {}
11+
}
12+
}
13+
14+
$outer = new \ReflectionClass(Outer::class);
15+
$ref = new \ReflectionClass('n\s\Outer:>Middle:>Inner');
16+
var_dump($ref->getName());
17+
var_dump($ref->getShortName());
18+
var_dump($ref->isInnerClass());
19+
var_dump($outer->isInnerClass());
20+
var_dump($ref->isPrivate());
21+
var_dump($ref->isProtected());
22+
var_dump($ref->isPublic());
23+
?>
24+
--EXPECT--
25+
string(24) "n\s\Outer:>Middle:>Inner"
26+
string(20) "Outer:>Middle:>Inner"
27+
bool(true)
28+
bool(false)
29+
bool(false)
30+
bool(false)
31+
bool(true)

0 commit comments

Comments
 (0)