property_exists() fails to find protected properties from a parent class
| Bug #42211 | property_exists() fails to find protected properties from a parent class | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2007-08-05 14:14 UTC | Modified: | 2007-08-08 13:33 UTC |
|
||||||||||
| From: | dominics at gmail dot com | Assigned: | dmitry (profile) | |||||||||||
| Status: | Closed | Package: | Class/Object related | |||||||||||
| PHP Version: | 5.2.4RC1 | OS: | * | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2007-08-05 14:14 UTC] 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)
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2007-08-05 23:07 UTC] 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-08 13:33 UTC] dmitry@php.net