Req #49369 [Opn->Sus]: Change current(), key(), next(), etc. to check for Iterator

From: Date: Sun, 14 Jun 2020 16:37:34 +0000
Subject: Req #49369 [Opn->Sus]: Change current(), key(), next(), etc. to check for Iterator
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-227474@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=49369&edit=1 ID: 49369 Updated by: cmb@php.net Reported by: admin at ifyouwantblood dot de Summary: Change current(), key(), next(), etc. to check for Iterator -Status: Open +Status: Suspended Type: Feature/Change Request Package: Arrays related Operating System: * PHP Version: * Block user comment: N Private report: N New Comment: This feature request would require the deprecation of these functions working on the mangled property table of objects[1], and eventually removing support for that. Only after sufficient time has passed after this removal, changing the functionality could even be considered. And still, introducing this feature would require the RFC process[2]. For the time being, I'm suspending this ticket. [1] <https://wiki.php.net/rfc/deprecations_php_8_0#key_current_next_prev_reset_on_objects> [2] <https://wiki.php.net/rfc/howto> Previous Comments: ------------------------------------------------------------------------ [2016-12-09 12:32:54] tom at r dot je 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. ------------------------------------------------------------------------ [2012-12-09 01:48:58] levim@php.net Added bug #63678 as a duplicate. ------------------------------------------------------------------------ [2012-11-16 04:35:52] levim@php.net Sorry, I misunderstood the bug. Silly me. Ignore my above comments. ------------------------------------------------------------------------ [2012-11-16 04:33:26] levim@php.net Also, calling current($i) gets the current value of the object $i which is the array; it doesn't call $i->current. ------------------------------------------------------------------------ [2012-11-16 04:27:19] levim@php.net I'm not sure this is a php bug. Iterators should be rewound almost 100% of the time before being used, especially when using an array or an object that implements Iterator instead of IteratorAggregate. Maybe the documentation should try to make this better known? ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=49369 -- Edit this bug report at https://bugs.php.net/bug.php?id=49369&edit=1

« previous php.bugs (#227474) next »