php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64399 ArrayObject works with ArrayObject as input but not ArrayAccess
Submitted: 2013-03-09 09:13 UTC Modified: 2016-02-21 12:34 UTC
From: kotlyar dot maksim at gmail dot com Assigned:
Status: Wont fix Package: SPL related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-03-09 09:13 UTC] kotlyar dot maksim at gmail dot com
Description:
------------
This code sample works fine

<?php

$input = new \ArrayObject;
$input['whatever'] = 'foo';

$array = new \ArrayObject($input);
var_dump($array['whatever']);
// will it return foo? right!

so I expected that it should work same way for objects implemented ArrayAccess 
interface. But it is not true.

Test script:
---------------
https://gist.github.com/makasim/5123486

Expected result:
----------------
either YourCustomArrayObject::offsetExists or YourCustomArrayObject::offsetGet is 
called

It should output 1 or 2 and exit the script

Actual result:
--------------
PHP Notice:  Undefined index:  whatever in /home/maksim/test.php on line 57


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-21 12:34 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Type: Bug +Type: Feature/Change Request
 [2016-02-21 12:34 UTC] nikic@php.net
If you pass an object to ArrayObject (and it is not an ArrayObject or ArrayIterator itself), ArrayObject will work on the properties of the object.

We cannot reasonably support ArrayAccess objects, because ArrayObject requires more than this interface to provide its functionality. In particular, it would also need at least Traversable and Countable. However, even with all of these we still couldn't provide complete functionality: The sorting methods essentially require random-access iterators, a concept that doesn't even exist in PHP.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC