|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28872 accessing a __get (getter) method from object value fails
Submitted: 2004-06-21 19:35 UTC Modified: 2005-01-15 00:04 UTC
From: nick dot rich at textmarketer dot co dot uk Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.0.0RC3 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: nick dot rich at textmarketer dot co dot uk
New email:
PHP Version: OS:


 [2004-06-21 19:35 UTC] nick dot rich at textmarketer dot co dot uk
Trying to access a propery (via the get method) using a 
value from another object as the method name fails.

See code::
if the line /// fails
replaced with

$localVar = $config->methodnames[1];

It works correctly

Reproduce code:
class config
	public $methodnames = array("dog","cat");

class testClass
	function Cat(){
		echo "cat";
	function Dog(){
		echo "dog";
	function __get($method){

class main
	function __construct(){
		$config = new config();
		$test = new testClass();
		$test->$config->methodnames[1]; /// fails

$test = new main();

Expected result:

Actual result:
Fatal error: Call to undefined method testClass::
Object() in /Users/nicholasrich/Sites/bits/
testclass.php(18) : eval()'d code on line 1


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-21 20:59 UTC]
first of all __get() is supposed to return something.
 [2004-06-22 09:51 UTC] nick dot rich at textmarketer dot co dot uk
The point is that the __get ihis being passed an object 
ref instead of the value. If you change the test class 
Class TestClass
private static $data = array("dog"=>"dog","cat"=>"cat");

function __get($value)
return self::$data[$value];
It will give the same result
 [2004-07-09 00:07 UTC] csmoak at andrew dot cmu dot edu
This is a bogus bug.

The problem here is that you are trying to access properties in an improper way--pay attention to order of operations.

The interpreter will operate from left to right--that is, on the line marked /// fails in the example code, it will first access $test, then call __get() with the value of (string) $config, which is 'Object'. You should have also gotten an error similar to:

Notice: Object of class config to string conversion in C:\php5\test2.php on line 28

which shows that it is doing this cast to a string.

you are expecting it to evaluate something like:


when, in fact, php evaluates:


which is not a bug.
 [2005-01-15 00:04 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 21 11:01:27 2024 UTC