php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36571 __set() affects outer variable when overloading with "[]=" and "{}="
Submitted: 2006-03-01 14:55 UTC Modified: 2006-08-07 10:46 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:1 (33.3%)
From: cid73 at 163 dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.1.3RC3 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cid73 at 163 dot com
New email:
PHP Version: OS:

 

 [2006-03-01 14:55 UTC] cid73 at 163 dot com
Description:
------------
I'm not sure if there are inner operaters named "[]=" or "{}=", but it really goes wrong when overloading such operations.

Here's some demo code.

Reproduce code:
---------------
class Setter
{
    private $attributes;

    public function __construct($attributes) {
        $this->attributes = $attributes; }

    private function __set($var, $value) {
        $this->attributes[$var] = $value; }

    private function __get($var) {
        if ( isset($this->attributes[$var]) ) {
            return $this->attributes[$var]; }
        else {
            return null; }}
}

$a = array('a' => array('a', 'b', 'c'), 'b' => 'Hello');
$o = new Setter($a);
$o->a[0] = 'A';
$o->a[]  = 'd';
$o->b{0} = 'h';

print_r($a); // gotta check $a, $o is good

/* ouputs:
Array
(
    [a] => Array
        (
            [0] => A
            [1] => b
            [2] => c
            [3] => d
        )

    [b] => hello
)
*/

Expected result:
----------------
$a is unchangeable at the moment

Actual result:
--------------
$a was modified

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-11 11:37 UTC] sniper@php.net
Dmitry, this does not look right to me.
 [2006-04-12 09:59 UTC] dmitry@php.net
One more overloading bug.
It is unfixable at this moment :(

 [2006-08-01 22:49 UTC] tony2001@php.net
Not reproducible with 5.2-CVS.
 [2006-08-06 15:54 UTC] cid73 at 163 dot com
Are you sure it is fixed now? Did you test $o->a and $o->b, or  print_r($o)? :-)
 [2006-08-06 16:04 UTC] tony2001@php.net
Yes, I did test it with 5.2-dev.

Array
(
    [a] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [b] => Hello
)

 [2006-08-07 08:10 UTC] cid73 at 163 dot com
Excuse me, my purpose was obvioursly to create a setter class, and then after I do:

------------
$o = new Setter($a);
$o->a[0] = 'A';
$o->a[]  = 'd';
$o->b{0} = 'h';
print_r($o); // note: it's $o, not $a
---------------

I think the result should now be:

Setter Object
(
    [attributes:private] => Array
        (
            [a] => Array
                (
                    [0] => A
                    [1] => b
                    [2] => c
                    [3] => d
                )

            [b] => hello
        )

)

The old problem was "$a was modified", this problem has been fixed; but the problem now goes to $o: all the assignments don't work. Let's see what I get:

Setter Object
(
    [attributes:private] => Array
        (
            [a] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                )

            [b] => Hello
        )

)

I tested it with 5.2-dev
 [2006-08-07 08:42 UTC] tony2001@php.net
$object->a = value;
and 
$object->b[] = value;
are completely different things.

For the second line to work __get() must return by reference, but __get() always returns *by value*.
So this is the expected and correct result.
 [2006-08-07 10:46 UTC] cid73 at 163 dot com
All right, now I know how to do, thanks a lot!
BTW, would you consider always pass arrays by reference like many other languages do?
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Mar 07 02:01:25 2021 UTC