Skip to content

Commit 5eec334

Browse files
committed
add reflection support
1 parent 8502401 commit 5eec334

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

ext/reflection/php_reflection.c

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

4078+
/* {{{ Returns whether the class is private */
4079+
ZEND_METHOD(ReflectionClass, isPrivate)
4080+
{
4081+
reflection_object *intern;
4082+
zend_class_entry *ce;
4083+
4084+
ZEND_PARSE_PARAMETERS_NONE();
4085+
GET_REFLECTION_OBJECT_PTR(ce);
4086+
RETURN_BOOL(ce->required_scope && ce->required_scope_absolute);
4087+
}
4088+
/* }}} */
4089+
4090+
/* {{{ Returns true if the class is protected */
4091+
ZEND_METHOD(ReflectionClass, isProtected)
4092+
{
4093+
reflection_object *intern;
4094+
zend_class_entry *ce;
4095+
4096+
ZEND_PARSE_PARAMETERS_NONE();
4097+
GET_REFLECTION_OBJECT_PTR(ce);
4098+
RETURN_BOOL(ce->required_scope && !ce->required_scope_absolute);
4099+
}
4100+
/* }}} */
4101+
4102+
/* {{{ Returns true if the class is public */
4103+
ZEND_METHOD(ReflectionClass, isPublic)
4104+
{
4105+
reflection_object *intern;
4106+
zend_class_entry *ce;
4107+
4108+
ZEND_PARSE_PARAMETERS_NONE();
4109+
GET_REFLECTION_OBJECT_PTR(ce);
4110+
RETURN_BOOL(!ce->required_scope);
4111+
}
4112+
/* }}} */
4113+
4114+
/* {{{ Returns whether the current class is an inner class */
4115+
ZEND_METHOD(ReflectionClass, isInnerClass)
4116+
{
4117+
reflection_object *intern;
4118+
zend_class_entry *ce;
4119+
ZEND_PARSE_PARAMETERS_NONE();
4120+
4121+
GET_REFLECTION_OBJECT_PTR(ce);
4122+
4123+
RETURN_BOOL(ce->lexical_scope && ce->lexical_scope->type != ZEND_NAMESPACE_CLASS);
4124+
}
4125+
/* }}} */
4126+
40784127
/* {{{ Returns an associative array containing all static property values of the class */
40794128
ZEND_METHOD(ReflectionClass, getStaticProperties)
40804129
{

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 isPublic(): bool {}
441+
442+
public function isProtected(): 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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
private class PrivateMiddle {}
13+
protected class ProtectedMiddle {}
14+
}
15+
16+
$outer = new \ReflectionClass(Outer::class);
17+
18+
19+
function details($ref) {
20+
echo "Details for $ref\n";
21+
$ref = new \ReflectionClass($ref);
22+
var_dump($ref->getName());
23+
var_dump($ref->getShortName());
24+
var_dump($ref->isInnerClass());
25+
var_dump($ref->isPrivate());
26+
var_dump($ref->isProtected());
27+
var_dump($ref->isPublic());
28+
}
29+
30+
details(Outer::class);
31+
details('n\s\Outer\Middle\Inner');
32+
details('n\s\Outer\PrivateMiddle');
33+
details('n\s\Outer\ProtectedMiddle');
34+
35+
?>
36+
--EXPECT--
37+
Details for n\s\Outer
38+
string(9) "n\s\Outer"
39+
string(5) "Outer"
40+
bool(false)
41+
bool(false)
42+
bool(false)
43+
bool(true)
44+
Details for n\s\Outer\Middle\Inner
45+
string(22) "n\s\Outer\Middle\Inner"
46+
string(5) "Inner"
47+
bool(true)
48+
bool(false)
49+
bool(false)
50+
bool(true)
51+
Details for n\s\Outer\PrivateMiddle
52+
string(23) "n\s\Outer\PrivateMiddle"
53+
string(13) "PrivateMiddle"
54+
bool(true)
55+
bool(true)
56+
bool(false)
57+
bool(false)
58+
Details for n\s\Outer\ProtectedMiddle
59+
string(25) "n\s\Outer\ProtectedMiddle"
60+
string(15) "ProtectedMiddle"
61+
bool(true)
62+
bool(false)
63+
bool(true)
64+
bool(false)

0 commit comments

Comments
 (0)