#28872 [Com]: accessing a __get (getter) method from object value fails

From: Date: Thu, 08 Jul 2004 22:07:31 +0000
Subject: #28872 [Com]: accessing a __get (getter) method from object value fails
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-61940@lists.php.net to get a copy of this message
ID: 28872 Comment by: csmoak at andrew dot cmu dot edu Reported By: nick dot rich at textmarketer dot co dot uk Status: Open Bug Type: Class/Object related Operating System: * PHP Version: 5.0.0RC3 New Comment: 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: $test->{$config->methodnames[1]}; when, in fact, php evaluates: {$test->$config}->methodnames[1]; which is not a bug. Previous Comments: ------------------------------------------------------------------------ [2004-06-22 09:51:42] 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 to: Class TestClass { private static $data = array("dog"=>"dog","cat"=>"cat"); function __get($value) { return self::$data[$value]; } } It will give the same result ------------------------------------------------------------------------ [2004-06-21 20:59:34] helly@php.net first of all __get() is supposed to return something. ------------------------------------------------------------------------ [2004-06-21 19:35:32] nick dot rich at textmarketer dot co dot uk Description: ------------ 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 is replaced with $localVar = $config->methodnames[1]; $test->$localVar 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){ eval("\$this->\$method();"); } } class main { function __construct(){ $config = new config(); $test = new testClass(); $test->$config->methodnames[1]; /// fails } } $test = new main(); Expected result: ---------------- cat Actual result: -------------- Fatal error: Call to undefined method testClass:: Object() in /Users/nicholasrich/Sites/bits/ testclass.php(18) : eval()'d code on line 1 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=28872&edit=1

« previous php.bugs (#61940) next »