php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33891 make array implements traversable, ArrayAccess and Countable
Submitted: 2005-07-27 22:14 UTC Modified: 2010-03-13 18:33 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: marcos dot neves at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.13 5.3.2 OS: any
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: marcos dot neves at gmail dot com
New email:
PHP Version: OS:

 

 [2005-07-27 22:14 UTC] marcos dot neves at gmail dot com
Description:
------------
Traversable is an internal interface that says "I can be used in a foreach". Since array can be used too, would be nice if array be accepted internally in parameters that expect traversable.

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

function acceptArray(array $t) {
	echo "\n".__FUNCTION__."\n";
	foreach($t as $k=>$v) {
		echo "$k=>$v\n";
	}
}

function acceptTraversable(Traversable $t) {
	echo "\n".__FUNCTION__."\n";
	foreach($t as $k=>$v) {
		echo "$k=>$v\n";
	}
}

$test = array("is", "array", "traversable", "too", "?");


acceptArray($test); // ok

acceptTraversable(new ArrayIterator($test)); // ok

acceptTraversable($test); // would be nice

?>

Expected result:
----------------
acceptTraversable($test); shoud accept an array, since it's too a Traversable structure(can be used in a foreach).

Actual result:
--------------
Fatal error: Argument 1 must be an object of class Traversable

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-13 18:08 UTC] wrzasq at gmail dot com
It should also expose interfaces ArrayAccess and Countable. It's very frustrating when you use type hinting to force iterable structures or array-like accessible and it comes out that you can't pass array themselves.
 [2010-03-13 18:17 UTC] fa@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-03-13 18:17 UTC] fa@php.net
Please try 5.3.2 or 5.2.13.
 [2010-03-13 18:33 UTC] marcos dot neves at gmail dot com
Still not working:
<?

function loop(Traversable $traversable)
{
  foreach($traversable as $k => $v)
    echo "$k => $v\n";
}

$primos = explode(" ", "1 2 3 5 7 11 13 17 19");

// loop($primos); // does not work
loop(new ArrayIterator($primos)); // works

?>
 [2012-10-07 19:41 UTC] arvenil at klecza dot pl
In other words using Traversable interface in php is useless.

There is no way to check if given variable is traversable. You need to drop type hinting and check if ($a instanceof Traversable || is_array($a)).

I understand that internally array isn't an object - but this is not excuse to make programmer life harder. If You test if something is Traversable by foreach then you don't care what this is.

Well at least foreach doesn't care if it get array or object - so this is possible! But if you still refuse to fix this bug then please make foreach work only for arrays and create another foreach for object e.g. foreachobject - at least language will be consistent.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 22:01:34 2025 UTC