php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
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
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
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

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC