php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44784 "Undefined property" notice when accessing property in __get and __set
Submitted: 2008-04-20 01:17 UTC Modified: 2008-04-20 10:53 UTC
From: marvinpaul at mapkreke dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: Windows Vista
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: marvinpaul at mapkreke dot com
New email:
PHP Version: OS:

 

 [2008-04-20 01:17 UTC] marvinpaul at mapkreke dot com
Description:
------------
If you have a child class that inherits from a parent class that implements __get and __set and you access a private property from within these functions, then you will get a notice "Undefined property" when trying to access this property from an instance of the child class. If you try accessing the properties from an instance of the parent class, it works as expected.

Reproduce code:
---------------
class ParentClass
{
    private $parentMessage = "Hello, from parent!";

    private function __get($property)
    {
        echo "__get($$property)\n";

        return $this->$property;
    }
    
    private function __set($property, $value)
    {
        echo "__set($$property, $value)\n";

        $this->$property = $value;
    }
}

class ChildClass extends ParentClass
{
    private $childMessage = "Hello, from child!";
}

$parent = new ParentClass();
echo $parent->parentMessage . "\n";

$child = new ChildClass();
echo $child->childMessage . "\n";

Expected result:
----------------
__get($parentMessage)
Hello, from parent!
__get($childMessage)
Hello, from child!


Actual result:
--------------
__get($parentMessage)
Hello, from parent!
__get($childMessage)
Notice:  Undefined property:  ChildClass::$childMessage in C:\www\bug.php on line 11


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-20 10:53 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

That's expected, from the context of ParentClass::__get, any private property of the child class won't be accessible.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Oct 24 04:03:35 2021 UTC