|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-08-26 10:34 UTC] admin at ifyouwantblood dot de
Description:
------------
it would be helpful for chained Iterators, if the default array functions would check if an given object is an instanceof Iterator and react appropriate.
thus key() calling object->key(), current() calling object->current() and so on.
Reproduce code:
---------------
<?php
class iterator_array implements Iterator
{
protected $aarray;
public function __construct($array)
{
$this->aarray=$array;
}
public function key()
{
return key($this->aarray);
}
public function current()
{
return current($this->aarray);
}
public function valid()
{
return (current($this->aarray)!==FALSE);
}
public function next()
{
next($this->aarray);
}
public function rewind()
{
reset($this->aarray);
}
}
$i=new iterator_array(Array(1,2));
var_dump(current($i));
var_dump(key($i));
next($i);
var_dump($current($i));
var_dump(key($i));
Expected result:
----------------
int(1)
int(0)
int(2)
int(1)
Actual result:
--------------
array(6) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
}
string(9) "�*�aarray"
bool(false)
NULL
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 20:00:01 2025 UTC |
Now that in 7.1 we can type hint iterable this would be very useful. function foo(iterable $i) { $first = current($i); $last = end($i); } Because $i is an iterator or an array, we can't differentiate between $i->current() and current($i) we'd need to do: function foo(iterable $i) { if (is_array($i)) { $first = current($i); $last = end($i); } else if ($i instanceof Traversable) { $first = $i->current(); foreach ($i as $x) { $last = $x; } } } It would be a lot simpler if this logic was abstracted into the various functions so they can work with either Traversable or array types.