Edit report at http://bugs.php.net/bug.php?id=51566&edit=1
ID: 51566 Updated by: ras...@php.net Reported by: link at dtalbert dot com Summary: __set() is called twice for each assignment operation and overwrites values -Status: Open +Status: Bogus Type: Bug Package: Class/Object related Operating System: CentOS PHP Version: 5.2.13 New Comment: You are doing that to yourself. You meant to do: $tmp = $this->$key; $tmp['value'] = $value; Otherwise it is going to do the $key['value'] part first which ends up being the same as $key[0] where $key = 'privVarName' so index 0 of that string is going to be 'p'. Previous Comments: ------------------------------------------------------------------------ [2010-04-15 23:56:37] link at dtalbert dot com Description: ------------ my PHP version is actually 5.2.6 I have overloaded the __set() and __get() functions in one of my classes and put logging statements around some test assignment lines. Here's an abbreviated version of that class: class MyClass { private privVarName = array('value' => null); public function __set($key, $value) { $this->$key['value'] = $value; } public function __get($key) { return $this->$key['value']; } } When I execute a line like: $instanceOfMyClass->privVarName = 17; My log will show me that the __set() function was called twice; once with the name "privVarName", and again with the name "p". It is always the first letter of the variable name and it is always set to the same value as I gave to the full variable name. Additionally, if I add another line like: $instanceOfMyClass->plarg = 4; then __set is again called twice (for "plarg" and for "p") and the value of $instanceOfMyClass->privVarName becomes 4. When I read the values for each member var back from the class they are set to whatever the last variable name with the same first letter was set to. Also, it does the same double calling and overwriting for variables that are not declared in the class file. The double calling and overwriting of same-first-letter variable names does not happen if I change my __set() and __get() functions to not use arrays, as in the following: public function __set($key, $value) { $this->$key = $value; } public function __get($key) { return $this->$key; } but of course this doesn't do what I need. If I needed this behavior I would just declare my member variables to be public and not overload the __set() and __get() at all. Expected result: ---------------- I expect it to call __set() only once. I expect that setting 2 different member variables to different values will not affect each other. Actual result: -------------- __set() is called twice for every assignment statement executed, the second time with the variable name being just the first letter of the actual variable name. all member variables with the same first letter in their names are overwritten by subsequent calls to __set() ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=51566&edit=1