note 106362 modified in language.oop5.overloading by danbrown

From: Date: Mon, 31 Oct 2011 15:13:56 +0000
Subject: note 106362 modified in language.oop5.overloading by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184258@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. <?php 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. --was-- 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. http://php.net/manual/en/language.oop5.overloading.php

« previous php.notes (#184258) next »