|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 18:00:02 2025 UTC |
Yes, I did test it with 5.2-dev. Array ( [a] => Array ( [0] => a [1] => b [2] => c ) [b] => Hello )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