Req #69988 [Fbk->NoF]: foreach ... as long as ...
| From: | php-bugs at lists dot php dot net | Date: | Sun, 30 Aug 2015 04:22:16 +0000 |
| Subject: | Req #69988 [Fbk->NoF]: foreach ... as long as ... | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195617@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69988&edit=1
ID: 69988
Updated by: php-bugs@lists.php.net
Reported by: w-p at dds dot nl
Summary: foreach ... as long as ...
-Status: Feedback
+Status: No Feedback
Type: Feature/Change Request
Package: *General Issues
PHP Version: Irrelevant
Private report: N
New Comment:
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
Previous Comments:
------------------------------------------------------------------------
[2015-08-20 01:19:40] kalle@php.net
That seems like something you can already easily do with a for-loop (given the array is indexed,
else use the array functions to advance the internal pointer):
<?php
$found = false;
for($x = 0, $l = sizeof($elements); $x < $l && !$found; ++$x)
{
if($elements[$x]->getFullName() == 'Rasmus Lerdorf')
{
$found = true;
}
}
if($found)
{
return($elements[$x]);
}
// ... ??? ...
?>
------------------------------------------------------------------------
[2015-07-03 12:06:31] w-p at dds dot nl
Description:
------------
In many languages, as in PHP, the is a loop to iterate over all elements of a collection. Many
languages also provide a mechanism to prematurely shut this loop down.
That is odd. By using a foreach loop, the programmer communicates that he will use the loop for all
the elements. But he doesn't. A common case is looking for an element in a collection in a way
that the collection itself does not support. Upon finding the element, the loop is prematurely
terminated.
The problem is that the shut-down of the loop is in fact a go-to statement (go to the first
statement after the loop) and severely frustrates refactoring. Exit, break and return statements
(the hidden go-to's) cannot be safely taken out of a method and be extracted to another one.
It would be better if there was a loop statement that provides the loop the programmer is actually
after: a foreach-with-condition loop. This communicates the intention better, and can always be
indented properly (how do you indent a hidden go-to statement?)
Off course, there are more uses than looking up something. Like performing actions on only those
elements where it makes sense, for example spell-checking on only those elements that have text. In
any case, we would have a structure statement that communicates what the loop is doing.
In the example below, I wrote "as long as" in keywords. This is so unlike the rest of PHP
that I expect that a symbol would be less out of place. I am thinking of a questing mark or a double
question mark.
Test script:
---------------
$objResult = NULL;
$blnFound = FALSE;
foreach($arrElements => $objElement as long as (! $blnFound)):
if($objElement->FullName()=='What I am looking for'):
$objResult = $objElement;
$blnFound = TRUE;
endif;
endforeach;
return $objResult;
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69988&edit=1