php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44793 ArrayObject does not report as Iterator
Submitted: 2008-04-21 12:52 UTC Modified: 2008-05-25 11:47 UTC
Votes:5
Avg. Score:4.6 ± 0.5
Reproduced:5 of 5 (100.0%)
Same Version:5 (100.0%)
Same OS:2 (40.0%)
From: matthew at zend dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5+ OS: *
Private report: No CVE-ID: None
 [2008-04-21 12:52 UTC] matthew at zend dot com
Description:
------------
ArrayObject extends ArrayAccess, which implements ArrayIterator and in turn Iterator. However, when checking to see if a concrete instance of ArrayObject implements Iterator, the return is false.

Reproduce code:
---------------
$o = new ArrayObject(array());
if ($o instanceof Iterator) {
    echo "Instance of Iterator";
} else {
    echo "Failed";
}

Expected result:
----------------
Instance of Iterator

Actual result:
--------------
Failed

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-21 15:51 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

class ArrayObject implements IteratorAggregate, Traversable, ArrayAccess, Serializable, Countable.

Traversable is not the same as Iterator: Traversable allows an object to be magically iterated by, i.e foreach, while Iterator explicitly provides userland iteration interface through next/valid/current/key/rewind.
 [2008-04-21 16:03 UTC] matthew at zend dot com
This is a bug in SPL, which is bundled in PHP core; additionally, it exposes a potential issue with how inheritence is resolved in the language. Please re-open.
 [2008-04-21 16:07 UTC] weierophinney@php.net
Re-opening
 [2008-04-21 16:12 UTC] weierophinney@php.net
Actually, ArrayAccess does not implment ArrayIterator, but vice versa. ArrayObject therefor does not implement Iterator.
 [2008-05-25 11:47 UTC] helly@php.net
For you rand everybodies education, there are two very useful commands as shown below. The second shows that ArrayAccess does not implement any Interface at all. The first shows that ArrayObject and ArrayIterator are on the same level. Doing 'php --rc ArrayIterator' and 'php --rc ArrayObject' you will find that the former implement Iterator while as the second implements IteratorAggregate which in turn does not inherit Iterator.

[marcus@zaphod php-cvs]$ php ext/spl/examples/class_tree.php ArrayAccess
make: `sapi/cli/php' is up to date.
ArrayAccess
|-ArrayIterator
| \-RecursiveArrayIterator (RecursiveIterator)
|   \-SubClasses
|-ArrayObject
|-CachingIterator (ArrayAccess, Countable)
| \-RecursiveCachingIterator (RecursiveIterator)
|-SplDoublyLinkedList
| |-SplQueue
| \-SplStack
\-SplObjectStorage
[marcus@zaphod php-cvs]$ php --rc ArrayAccess
make: `sapi/cli/php' is up to date.
Interface [ <internal> interface ArrayAccess ] {

....
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 12:01:27 2024 UTC