#42211 [Asn->Csd]: property_exists() fails to find protected properties from a parent class
ID: 42211
Updated by: dmitry@php.net
Reported By: dominics at gmail dot com
-Status: Assigned
+Status: Closed
Bug Type: Class/Object related
Operating System: *
PHP Version: 5.2.4RC1
Assigned To: dmitry
New Comment:
This bug has been fixed in CVS.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2007-08-05 23:07:45] crrodriguez at suse dot de
If this is not a bug, there is a bug in the documentation ;)
--TEST--
#42211 property_exists( ) fails to find protected properties from a
parent class
--FILE--
<?php
class A {
function foo() {
echo __CLASS__ ."\n";
var_dump(property_exists('B', 'publicBar'));
var_dump(property_exists('B', 'protectedBar'));
var_dump(property_exists('B', 'privateBar'));
}
}
class B extends A {
public $publicBar;
protected $protectedBar;
private $privateBar;
function __construct()
{ echo __CLASS__ ."\n";
var_dump(property_exists('B', 'publicBar'));
var_dump(property_exists('B', 'protectedBar'));
var_dump(property_exists('B', 'privateBar'));
}
}
$b = new B();
$b->foo();
echo "No scope\n";
var_dump(property_exists('B', 'publicBar'));
var_dump(property_exists('B', 'protectedBar'));
var_dump(property_exists('B', 'privateBar'));
?>
--EXPECT--
B
bool(true)
bool(true)
bool(true)
A
bool(true)
bool(true)
bool(false)
No scope
bool(true)
bool(false)
bool(false)
Index: Zend/zend_builtin_functions.c
===================================================================
RCS file: /repository/ZendEngine2/zend_builtin_functions.c,v
retrieving revision 1.277.2.12.2.22
diff -u -p -r1.277.2.12.2.22 zend_builtin_functions.c
--- Zend/zend_builtin_functions.c 2 Aug 2007 20:32:44 -0000
1.277.2.12.2.22
+++ Zend/zend_builtin_functions.c 5 Aug 2007 23:06:02 -0000
@@ -974,7 +974,7 @@ ZEND_FUNCTION(property_exists)
if (!(property_info = zend_get_property_info(ce,
*property, 1 TSRMLS_CC)) || property_info == &EG(std_property_info)) {
RETURN_FALSE;
}
- if (property_info->flags & ZEND_ACC_PUBLIC) {
+ if (property_info->flags & ZEND_ACC_PUBLIC ||
((property_info->flags & ZEND_ACC_PROTECTED) &&
zend_check_protected(property_info->ce, EG(scope)))) {
RETURN_TRUE;
}
zend_unmangle_property_name(property_info->name,
property_info->name_length, &class_name, &prop_name);
ps: will be nice to have a "attachment feature"
Cheers
Cristian (aka judas_isc ;) )
------------------------------------------------------------------------
[2007-08-05 14:14:36] dominics at gmail dot com
Description:
------------
When using property_exists() from a parent class, and checking for the
existence of a protected property in the subclass, property_exists()
fails to find the property.
The documentation for property_exists() says that the property must be
'accessible from the current scope'. In this case, the property _is_,
because A is a parent class and protected visibility is defined in the
documentation as 'limits access to inherited and parent classes'.
As further evidence that protectedBar is accessible from foo(), trying
to manipulate privateBar causes an error, while manipulating
protectedBar doesn't.
Reproduce code:
---------------
<?php
class A {
function foo() {
var_dump(property_exists('B', 'publicBar'));
var_dump(property_exists('B', 'protectedBar'));
var_dump(property_exists('B', 'privateBar'));
}
}
class B extends A {
public $publicBar;
protected $protectedBar;
private $privateBar;
}
$b = new B();
$b->foo();
Expected result:
----------------
bool(true) bool(true) bool(false)
Actual result:
--------------
bool(true) bool(false) bool(false)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42211&edit=1
Thread (4 messages)