note 69370 added to language.oop5.overloading

From: Date: Sat, 02 Sep 2006 16:12:49 +0000
Subject: note 69370 added to language.oop5.overloading
Groups: php.notes 
Request: Send a blank email to php-notes+get-116631@lists.php.net to get a copy of this message
$myclass->foo['bar'] = 'baz'; When overriding __get and __set, the above code can work (as expected) but it depends on your __get implementation rather than your __set. In fact, __set is never called with the above code. It appears that PHP (at least as of 5.1) uses a reference to whatever was returned by __get. To be more verbose, the above code is essentially identical to: $tmp_array = &$myclass->foo; $tmp_array['bar'] = 'baz'; unset($tmp_array); Therefore, the above won't do anything if your __get implementation resembles this: function __get($name) { return array_key_exists($name, $this->values) ? $this->values[$name] : null; } You will actually need to set the value in __get and return that, as in the following code: function __get($name) { if (!array_key_exists($name, $this->values)) $this->values[$name] = null; return $this->values[$name]; } ---- Server IP: 210.138.109.182 Probable Submitter: 222.2.41.224 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php Edit -- https://master.php.net/note/edit/69370 Del: integrated -- https://master.php.net/note/delete/69370/integrated Del: useless -- https://master.php.net/note/delete/69370/useless Del: bad code -- https://master.php.net/note/delete/69370/bad+code Del: spam -- https://master.php.net/note/delete/69370/spam Del: non-english -- https://master.php.net/note/delete/69370/non-english Del: in docs -- https://master.php.net/note/delete/69370/in+docs Del: other reasons-- https://master.php.net/note/delete/69370 Reject -- https://master.php.net/note/reject/69370 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116631) next »