note 106362 added to language.oop5.overloading

From: Date: Mon, 31 Oct 2011 09:57:59 +0000
Subject: note 106362 added to language.oop5.overloading
Groups: php.notes 
Request: Send a blank email to php-notes+get-184241@lists.php.net to get a copy of this message
It may be important to note that when __set($name, $value) is called, $name gets entered into the symbol table for that call stack. This means that if a property is set within __set, it will only work if that property's name appears in the call stack (ie if it matches $name). If not, __set will be called again on the new property recursively. class test { function __set($name, $value) { echo "__set($name, $value);\n"; $this->prop3 = $value; // This will call __set(prop3, value) $this->prop2 = $value; // This will call __set(prop2, value) $this->prop1 = $value; // This will NOT call __set(prop1, value) because $name == prop1. } } $test = new test; $test->prop1 = 'value'; It could be thought of like this: within the function __set($name, $value), $this->[property name] = [value] will recurse unless property name == $name. Within the call stack of __set, __set will never recurse on the same name twice. Once you leave __set (without actually creating the property), the call stack ends and all bets are off. What this means: You cannot do things like setting an (uppercase) property to lowercase within __set without expecting __set to be called twice. If you were to add an underscore to the property name, you can expect an infinite recursion loop. I hope someone finds this useful. It drove me nuts for a few hours. ---- Server IP: 69.147.83.197 Probable Submitter: 68.2.121.250 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php Edit -- https://master.php.net/note/edit/106362 Del: integrated -- https://master.php.net/note/delete/106362/integrated Del: useless -- https://master.php.net/note/delete/106362/useless Del: bad code -- https://master.php.net/note/delete/106362/bad+code Del: spam -- https://master.php.net/note/delete/106362/spam Del: non-english -- https://master.php.net/note/delete/106362/non-english Del: in docs -- https://master.php.net/note/delete/106362/in+docs Del: other reasons-- https://master.php.net/note/delete/106362 Reject -- https://master.php.net/note/reject/106362 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184241) next »