|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 15:00:01 2025 UTC |
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 ;) )