Bug #51566 [Bgs]: __set() is called twice for each assignment operation and overwrites values
Edit report at http://bugs.php.net/bug.php?id=51566&edit=1 ID: 51566 User updated by: link at dtalbert dot com Reported by: link at dtalbert dot com Summary: __set() is called twice for each assignment operation and overwrites values Status: Bogus Type: Bug Package: Class/Object related Operating System: CentOS PHP Version: 5.2.13 New Comment: Thank you for the prompt reply. Your solution was, of course, correct. My fault entirely, thanks for helping move on. Previous Comments: [2010-04-16 00:05:59] ras...@php.net 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'. ------------ [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
[PHP-BUG] Bug #51566 [NEW]: __set() is called twice for each assignment operation and overwrites values
From: Operating system: CentOS PHP version: 5.2.13 Package: Class/Object related Bug Type: Bug Bug description:__set() is called twice for each assignment operation and overwrites values 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 bug report at http://bugs.php.net/bug.php?id=51566&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51566&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51566&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51566&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51566&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51566&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51566&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51566&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51566&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51566&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51566&r=support Expected behavior: http://bugs.php.net/fix.php?id=51566&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51566&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51566&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51566&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51566&r=php4 Daylight Savings:http://bugs.php.net/fix.php?id=51566&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51566&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51566&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51566&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51566&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51566&r=mysqlcfg