Skip to content

Commit 8e91d46

Browse files
committed
- Fixed bug #49719 (ReflectionClass::hasProperty returns true for a private property in base class)
1 parent 94cb011 commit 8e91d46

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PHP NEWS
1616
(Pierre)
1717

1818
- Fixed bug #50023 (pdo_mysql doesn't use PHP_MYSQL_UNIX_SOCK_ADDR). (Ilia)
19+
- Fixed bug #49719 (ReflectionClass::hasProperty returns true for a private
20+
property in base class). (Felipe)
1921
- Fixed bug #49142 (crash when exception thrown from __tostring()).
2022
(David Soria Parra)
2123
- Fixed bug #49990 (SNMP3 warning message about security level printed twice).

ext/reflection/php_reflection.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -3512,6 +3512,7 @@ ZEND_METHOD(reflection_class, getMethods)
35123512
ZEND_METHOD(reflection_class, hasProperty)
35133513
{
35143514
reflection_object *intern;
3515+
zend_property_info *property_info;
35153516
zend_class_entry *ce;
35163517
char *name;
35173518
int name_len;
@@ -3523,11 +3524,13 @@ ZEND_METHOD(reflection_class, hasProperty)
35233524
}
35243525

35253526
GET_REFLECTION_OBJECT_PTR(ce);
3526-
if (zend_hash_exists(&ce->properties_info, name, name_len + 1)) {
3527+
if (zend_hash_find(&ce->properties_info, name, name_len+1, (void **) &property_info) == SUCCESS) {
3528+
if (property_info->flags & ZEND_ACC_SHADOW) {
3529+
RETURN_FALSE;
3530+
}
35273531
RETURN_TRUE;
35283532
} else {
3529-
if (intern->obj && Z_OBJ_HANDLER_P(intern->obj, has_property))
3530-
{
3533+
if (intern->obj && Z_OBJ_HANDLER_P(intern->obj, has_property)) {
35313534
MAKE_STD_ZVAL(property);
35323535
ZVAL_STRINGL(property, name, name_len, 1);
35333536
if (Z_OBJ_HANDLER_P(intern->obj, has_property)(intern->obj, property, 0 TSRMLS_CC)) {

ext/reflection/tests/ReflectionClass_hasProperty_001.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Reflecting on class privf:
6969
--> Check for doesntExist: bool(false)
7070
Reflecting on class subprivf:
7171
--> Check for s: bool(true)
72-
--> Check for a: bool(true)
72+
--> Check for a: bool(false)
7373
--> Check for A: bool(false)
7474
--> Check for doesntExist: bool(false)
7575

ext/reflection/tests/bug49719.phpt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
Bug #49719 (ReflectionClass::hasProperty returns true for a private property in base class)
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
private $a;
8+
}
9+
class B extends A {
10+
private $b;
11+
}
12+
13+
try {
14+
$b = new B;
15+
$ref = new ReflectionClass($b);
16+
17+
var_dump(property_exists('b', 'a'));
18+
var_dump(property_exists($b, 'a'));
19+
var_dump($ref->hasProperty('a'));
20+
var_dump($ref->getProperty('a'));
21+
} catch (Exception $e) {
22+
var_dump($e->getMessage());
23+
}
24+
25+
class A2 {
26+
private $a = 1;
27+
}
28+
29+
class B2 extends A2 {
30+
private $a = 2;
31+
}
32+
33+
$b2 = new ReflectionClass('B2');
34+
$prop = $b2->getProperty('a');
35+
$prop->setAccessible(true);
36+
var_dump($prop->getValue(new b2));
37+
38+
?>
39+
--EXPECTF--
40+
bool(false)
41+
bool(false)
42+
bool(false)
43+
%string|unicode%(25) "Property a does not exist"
44+
int(2)

0 commit comments

Comments
 (0)