|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 05:00:02 2025 UTC |
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 ?>