note 69361 added to language.oop5.magic

From: Date: Sat, 02 Sep 2006 07:32:13 +0000
Subject: note 69361 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-116605@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: 66.163.161.117 Probable Submitter: 221.186.146.128 (proxied: unknown) ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/69361 Del: integrated -- https://master.php.net/note/delete/69361/integrated Del: useless -- https://master.php.net/note/delete/69361/useless Del: bad code -- https://master.php.net/note/delete/69361/bad+code Del: spam -- https://master.php.net/note/delete/69361/spam Del: non-english -- https://master.php.net/note/delete/69361/non-english Del: in docs -- https://master.php.net/note/delete/69361/in+docs Del: other reasons-- https://master.php.net/note/delete/69361 Reject -- https://master.php.net/note/reject/69361 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116605) next »