php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34849 array_key_exists and isset misbehavior with ArrayAccess
Submitted: 2005-10-13 06:56 UTC Modified: 2015-02-23 10:26 UTC
From: tschundler at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2005-10-13 06:56 UTC] tschundler at gmail dot com
Description:
------------
Functions to test an array's content don't work consistently with ArrayAccess objects.

In the example, the object will return TRUE a request for any key. But, array_key_exists always returns FALSE, seemingly testing nothing, when it should be using the offsetExists method.

Additionally, isset() on an array will return FALSE if the value at that offset is NULL. But with an ArrayAccess object, it returns TRUE, using the offsetExists method.

Reproduce code:
---------------
class ArrayTest implements ArrayAccess {
    function offsetExists ($offset) {return TRUE;}
    function offsetGet ($offset) {return ($offset==0) ? NULL : $offset;}
    function offsetSet ($offset, $value) {}
    function offsetUnset ($offset) {}
}

echo "with ArrayAcces:\n";
$x=new ArrayTest();
var_dump(array_key_exists(0,$x));
var_dump(array_key_exists(1,$x));
var_dump(isset($x[0]));
var_dump(isset($x[1]));

echo "with array():\n";
$y=array(0=>NULL,1=>1);
var_dump(array_key_exists(0,$y));
var_dump(array_key_exists(1,$y));                         
var_dump(isset($y[0]));
var_dump(isset($y[1]));


Expected result:
----------------
with ArrayAcces:
bool(true)
bool(true)
bool(false)
bool(true)
with array():
bool(true)
bool(true)
bool(false)
bool(true)


Actual result:
--------------
with ArrayAcces:
bool(false)
bool(false)
bool(true)
bool(true)
with array():
bool(true)
bool(true)
bool(false)
bool(true)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-13 08:24 UTC] helly@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

ArrayAccess objects don't work in array_*() functions. You may want to turn this report into a feature request?
 [2011-07-11 14:17 UTC] jon at phpsitesolutions dot com
That's ridiculous, that ArrayAccess doesn't work with PHP's own array_* 
functions.
If that's the case, why provide ArrayAccess as an SPL class?
You'd think that SPL classes would have full functionality with native PHP, yet 
because someone was apparently lazy during implementation, it doesn't.

And so of course, 6 years later, this is still an issue that has not been 
rectified.

Now I see why PHP has such a bad reputation when compared against other quality 
languages...
 [2011-07-11 14:20 UTC] jon at phpsitesolutions dot com
BTW, that tidbit (array_* functions are not compatible with ArrayAccess) is not 
mentioned in the manual, which makes it pointless to point the bug reporter to 
the manual.

And no, I'm not submitting a bug request for that change to be added, as I'm not 
the person that:
- pointed the reporter back to the manual
- noted that array_* functions are incompatible

Seems the bug request should be on that person in that case.
 [2011-07-12 09:11 UTC] arpad@php.net
To clarify, objects do work with some array_* functions like array_key_exists() and array_walk(), but using the hashtable of the object's properties.

Changing these functions to use the ArrayAccess (and Traversable etc.) interface when present is possible but would be a BC break.
 [2015-02-23 08:51 UTC] thomas at gielfeldt dot dk
Could we create a new interface, e.g. ArrayAccessExtended, to implement the remaining array functionality?
 [2015-02-23 10:26 UTC] yohgaki@php.net
thomas, you should file new bug record for a feature request. 
Please search bug DB before creating new one.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon May 25 11:01:24 2020 UTC