php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51566 __set() is called twice for each assignment operation and overwrites values
Submitted: 2010-04-15 23:56 UTC Modified: 2010-04-16 14:53 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: link at dtalbert dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.13 OS: CentOS
Private report: No CVE-ID: None
 [2010-04-15 23:56 UTC] 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()

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-16 00:05 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-04-16 00:05 UTC] rasmus@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-16 14:53 UTC] link at dtalbert dot com
Thank you for the prompt reply. Your solution was, of course, correct. My fault entirely, thanks for helping move on.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC