#33931 [Opn->Ver]: __get and __set don't handle arrays correctly

From: Date: Sat, 30 Jul 2005 21:52:15 +0000
Subject: #33931 [Opn->Ver]: __get and __set don't handle arrays correctly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-82724@lists.php.net to get a copy of this message
ID: 33931 Updated by: sean@php.net Reported By: wkonkel at gmail dot com -Status: Open +Status: Verified Bug Type: *General Issues Operating System: n/a PHP Version: 5.0.4 New Comment: Happens to me on 5.0.3 as well. Shorter code: <?php class A { public function __set($key, $val) { $this->data[$key] = $val; } } $A = new A(); $A->foo['bar'] = 'oink'; $A->foo['bar1'] = 'oink1'; var_dump($undefined); $undefined[] = 'blah'; var_dump($undefined); ?> Outputs: NULL array(2) { ["bar1"]=> string(5) "oink1" [0]=> string(4) "blah" } Previous Comments: ------------------------------------------------------------------------ [2005-07-30 22:39:55] wkonkel at gmail dot com Description: ------------ The __set and __get don't seem to handle arrays correctly. What's even worse, is it adds data to memory which is allocated at a later time ($boz gets foo2 and foo3 of $myObj). The code example speaks for itself. Reproduce code: --------------- <? class buggy { private $data; function __set($key, $val) { $this->data[$key] = $val; } function __get($key) { return $this->data[$key]; } } $myObj = new buggy(); $myObj->somevar['foo1'] = 'bar1'; $myObj->somevar['foo2'] = 'bar2'; $myObj->somevar['foo3'] = 'bar3'; $boz[] = 'blah'; print_r($boz); ?> Expected result: ---------------- Array ( [0] => blah ) Actual result: -------------- Array ( [foo2] => bar2 [foo3] => bar3 [0] => blah ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=33931&edit=1

« previous php.bugs (#82724) next »