|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-04-16 00:05 UTC] rasmus@php.net
-Status: Open
+Status: Bogus
[2010-04-16 00:05 UTC] rasmus@php.net
[2010-04-16 14:53 UTC] link at dtalbert dot com
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 19:00:01 2025 UTC |
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()