php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48489 Modifying an array member of an ArrayObject doesn't pass through offsetGet/Set
Submitted: 2009-06-07 08:40 UTC Modified: 2009-06-07 11:46 UTC
From: angafenion at yahoo dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.3.0RC2 OS: win32
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: angafenion at yahoo dot com
New email:
PHP Version: OS:

 

 [2009-06-07 08:40 UTC] angafenion at yahoo dot com
Description:
------------
I overrode the methods of ArrayObject to normalize the keys before setting and getting members. It appears that the overridden methods are skipped when modifying an array member.

Reproduce code:
---------------
<?php

class foo extends ArrayObject
{
    public function __construct($data = array(),
                                $flags = self::ARRAY_AS_PROPS,
                                $iterator = "ArrayIterator")
    {
        parent::__construct($data, $flags, $iterator);
    }

    public function offsetSet($key, $val)
    {
        echo "offsetSet invoked\n";
        parent::offsetSet($key, $val);
    }

    public function offsetGet($key)
    {
        echo "offsetGet invoked\n";
        return parent::offsetGet($key);
    }

}

$foo = new foo();

$foo->baz = array();
echo count($foo->baz) . "\n";

echo "---\n";

$foo->baz['bar'] = 'foo';
echo count($foo->baz) . "\n";


Expected result:
----------------
offsetSet invoked
offsetGet invoked
0
---
offsetGet invoked  --[*]
offsetSet invoked  _/
offsetGet invoked
1


[*] actually, I'm not sure exactly what output to expect here, but at least one of these I think should appear

Actual result:
--------------
offsetSet invoked
offsetGet invoked
0
---
offsetGet invoked
1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-07 11:21 UTC] colder@php.net
This is expected, the statement:
$foo->baz['bar'] = 'far';
tries to affect the variable $foo->baz, and hence need to get it first.
This is why offsetGet is called. 

ArrayObject::offsetGet() uses various hacks to return a reference to the initial array, allowing modifications.
 [2009-06-07 11:46 UTC] angafenion at yahoo dot com
> tries to affect the variable $foo->baz, and hence need to get it first.
> This is why offsetGet is called. 

That's exactly what I want to happen, but offsetGet() isn't called when doing $foo->baz['bar'] = 'foo'. The last 'offsetGet invoked' line is because of the count($foo->baz) statement.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 15:01:36 2025 UTC