#36571 [Csd->Opn]: __set() affects outer variable when overloading with "[]=" and "{}="

From: Date: Sun, 06 Aug 2006 15:54:18 +0000
Subject: #36571 [Csd->Opn]: __set() affects outer variable when overloading with "[]=" and "{}="
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-100428@lists.php.net to get a copy of this message
ID: 36571 User updated by: cid73 at 163 dot com Reported By: cid73 at 163 dot com -Status: Closed +Status: Open Bug Type: Scripting Engine problem Operating System: * PHP Version: 5.1.3RC3 Assigned To: dmitry New Comment: Are you sure it is fixed now? Did you test $o->a and $o->b, or print_r($o)? :-) Previous Comments: ------------------------------------------------------------------------ [2006-08-01 22:49:53] tony2001@php.net Not reproducible with 5.2-CVS. ------------------------------------------------------------------------ [2006-04-12 09:59:05] dmitry@php.net One more overloading bug. It is unfixable at this moment :( ------------------------------------------------------------------------ [2006-04-11 11:37:18] sniper@php.net Dmitry, this does not look right to me. ------------------------------------------------------------------------ [2006-03-01 14:55:50] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=36571&edit=1

« previous php.bugs (#100428) next »