#45653 [Opn->Bgs]: ArrayAccess does not call offsetSet when an element is incremented

From: Date: Tue, 29 Jul 2008 12:14:51 +0000
Subject: #45653 [Opn->Bgs]: ArrayAccess does not call offsetSet when an element is incremented
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-127386@lists.php.net to get a copy of this message
ID: 45653 Updated by: colder@php.net -Summary: ArrayObject does not call offsetSet when an element is incremented Reported By: agalkin at agalkin dot ru -Status: Open +Status: Bogus -Bug Type: SPL related +Bug Type: Scripting Engine problem Operating System: Mac OS X 10.5.4 PHP Version: 5.2.6 New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is expected, indirect modifications through ArrayAccess is not supported (for now), that means $b =& $obj['foo']; $b = 2; $obj['foo']++ ++$obj['foo']; array_pop($obj['foo']); etc.. won't work The fact that += works is an exception, as it's virtually $obj['foo'] = $obj['foo'] + 1; Previous Comments: ------------------------------------------------------------------------ [2008-07-29 11:11:04] agalkin at agalkin dot ru Description: ------------ When an element of ArrayObject is incremented (++ operation), the object does not call its offsetSet method. I've tried overloading all public methods in ArrayObject: offsetGet is called, but not offsetSet or any other. Same with decrementing (--). This behaviour makes it impossible to handle all array modifications by overloading ArrayObject methods in derived class. Reproduce code: --------------- class myArray extends ArrayIterator { public function offsetSet($index, $newval) { echo "offsetSet called\n"; return parent::offsetSet($index, $newval); } } $a = new myArray(array('x' => 1)); echo "x += 1\n"; $a['x'] += 1; // calls offsetGet, then offsetSet with updated value echo $a['x']."\n"; echo "x++\n"; $a['x']++; // only calls offsetGet echo $a['x']."\n"; // but the value gets incremented Expected result: ---------------- x += 1 offsetSet called 2 x++ offsetSet called 3 Actual result: -------------- x += 1 offsetSet called 2 x++ 3 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45653&edit=1

« previous php.bugs (#127386) next »