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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kotlyar dot maksim at gmail dot com
New email:
PHP Version: OS:

 

 [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: Sat Apr 27 01:01:30 2024 UTC