Skip to content

Commit d3b3666

Browse files
committed
add reflection support
1 parent df7f149 commit d3b3666

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
@@ -4138,6 +4138,55 @@ static void add_class_vars(zend_class_entry *ce, bool statics, zval *return_valu
41384138
}
41394139
/* }}} */
41404140

4141+
/* {{{ Returns whether the class is private */
4142+
ZEND_METHOD(ReflectionClass, isPrivate)
4143+
{
4144+
reflection_object *intern;
4145+
zend_class_entry *ce;
4146+
4147+
ZEND_PARSE_PARAMETERS_NONE();
4148+
GET_REFLECTION_OBJECT_PTR(ce);
4149+
RETURN_BOOL(ce->required_scope && ce->required_scope_absolute);
4150+
}
4151+
/* }}} */
4152+
4153+
/* {{{ Returns true if the class is protected */
4154+
ZEND_METHOD(ReflectionClass, isProtected)
4155+
{
4156+
reflection_object *intern;
4157+
zend_class_entry *ce;
4158+
4159+
ZEND_PARSE_PARAMETERS_NONE();
4160+
GET_REFLECTION_OBJECT_PTR(ce);
4161+
RETURN_BOOL(ce->required_scope && !ce->required_scope_absolute);
4162+
}
4163+
/* }}} */
4164+
4165+
/* {{{ Returns true if the class is public */
4166+
ZEND_METHOD(ReflectionClass, isPublic)
4167+
{
4168+
reflection_object *intern;
4169+
zend_class_entry *ce;
4170+
4171+
ZEND_PARSE_PARAMETERS_NONE();
4172+
GET_REFLECTION_OBJECT_PTR(ce);
4173+
RETURN_BOOL(!ce->required_scope);
4174+
}
4175+
/* }}} */
4176+
4177+
/* {{{ Returns whether the current class is an inner class */
4178+
ZEND_METHOD(ReflectionClass, isInnerClass)
4179+
{
4180+
reflection_object *intern;
4181+
zend_class_entry *ce;
4182+
ZEND_PARSE_PARAMETERS_NONE();
4183+
4184+
GET_REFLECTION_OBJECT_PTR(ce);
4185+
4186+
RETURN_BOOL(ce->lexical_scope && ce->lexical_scope->type != ZEND_NAMESPACE_CLASS);
4187+
}
4188+
/* }}} */
4189+
41414190
/* {{{ Returns an associative array containing all static property values of the class */
41424191
ZEND_METHOD(ReflectionClass, getStaticProperties)
41434192
{

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)