php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45653 ArrayAccess does not call offsetSet when an element is incremented
Submitted: 2008-07-29 11:11 UTC Modified: 2008-07-29 12:14 UTC
From: agalkin at agalkin dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Mac OS X 10.5.4
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: agalkin at agalkin dot ru
New email:
PHP Version: OS:

 

 [2008-07-29 11:11 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-29 12:14 UTC] colder@php.net
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;


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 07:01:29 2024 UTC