php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45097 Magic __get() can't access inherited properties
Submitted: 2008-05-26 18:54 UTC Modified: 2008-05-30 11:58 UTC
From: ndaugherty987 at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.6 OS: linux
Private report: No CVE-ID: None
 [2008-05-26 18:54 UTC] ndaugherty987 at gmail dot com
Description:
------------
The magic overloading function __get() can't seem to access inherited, non-visible properties. If you overload with __get() in a base class, then extend that class, the base class's __get() will return NULL when accessing an inaccessible property of the child class. The __get() function doesn't even see the property as being set (if you run an isset($name) in the __get() call, it returns false.

Reproduce code:
---------------
class testParent {
	function __get($name){
		return $this->$name;
	}
}

class testChild extends testParent {
	private $testVar = 'Bar';
}

$t = new testChild;

echo 'Foo: ' . $t->testVar;

Expected result:
----------------
You would expect the code to output 'Foo: Bar'. instead, it outputs 'Foo:', because the __get() in testParent apparently can't see the private vars in testChild.

Actual result:
--------------
Output's 'Foo:', withOUT the 'Bar'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-30 11:58 UTC] felipe@php.net
This is expected, use protected/public visibility.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 27 23:00:03 2025 UTC