Emulate goto functionality in PHP 5.2 - perhaps the best approach?

I wrote a script using my local PHP 5.3 installation using the goto . Having downloaded it to my web server, I had to find that my host was still installed with PHP 5.2 and, therefore, it did not support goto . So my question is how to emulate goto functionality in the following context:

 foo(); iterator_start: foreach ($array as $array_item) { switch ($array_item) { case A: foo(); break; case B: // Substitute array_splice($array, offset($array_item), 1, array(A, A, B, C)); // Restart iterator goto iterator_start; break; } } 

The idea is that the array should be modified according to a set of substitution rules. After the rule is executed (which can change any number of array elements at any position, replace them, even delete them), everything should start from scratch, because the entire array can be changed in an unpredictable way (thus, recursive replacement rules are allowed as Well). The solution I use, with goto just going to the beginning of the loop after each substitution, seems very simple and even quite clean for me, but, as I said, I can not use it on my web server.

Is there any replacement for goto here, or can the same task be performed in a completely different way (preferably without changing too much code)?

Any ideas are welcome.


Polite request: Please share me with lectures on the benefits or dangers of goto here. I read PHP and the goto instruction, which will be added in PHP 5.3 , and find out about the spaghetti code and all those models that are considered malicious letters of the 1980s. The discussion of goto alleged evil has nothing to do with my question, and treating any program design as "bad style" as such is simply a dogma that does not have a place in my programming ideology.

+4
source share
5 answers

You can use each() in a while loop. It uses the cursor of the internal array to retrieve the current element in the array and move the cursor to the next. When slicing an array, reset to the cursor to restart from the beginning of the array at the next iteration of the loop. The reset() call is probably not even required, perhaps this is a side effect of array_splice() , as it modifies the array.

 foo(); while (list($key, $array_item) = each($array)) { switch ($array_item) { case A: foo(); break; case B: // Substitute array_splice($array, offset($array_item), 1, array(A, A, B, C)); // Reset array cursor, this is probably not necessary reset($array); break; } } 
+2
source

recursion? Instead of goto iterator_start you transfer this loop to a function and call it recursively.

+1
source

Well, they say that any goto can be replaced with control structures such as for and while or functions.

In your case, I would use a function for this:

 function goto_substitute($array){ foreach ($array as $array_item) { switch ($array_item) { case A: foo(); return $array; case B: // Substitute array_splice($array, offset($array_item), 1, array(A, A, B, C)); // Restart iterator return goto_substitute($array) ; } } } 

Edit (to respond to the comment)

Functions make the code more readable. The best programming practice says that a function should do one thing and not be larger than the screen size (about 80-90 lines) - you have several replacement blocks - it seems logical to have several functions - if you call them intuitively, then someone will understand what is happening there

+1
source

Have you tried using include? I mean, you write your foreach code in a file called

foreach.php like this, instead of using goto, you simply use include ('foreach.php') as follows:

 foreach ($array as $array_item) { switch ($array_item) { case A: foo(); break; case B: // Substitute array_splice($array, offset($array_item), 1, array(A, A, B, C)); // Restart iterator include('foreach.php'); break; } 

}

0
source

goto is an evil artifact of ancient times. Why it is introduced in PHP 5.3, no one knows. Do not use it. Someday.

This is an excellent explanation of Dijkstra, which even suggests that some since 1959 have been unhappy with some. As there are more statements about the flow of control, there is even less need for goto. It may have specific applications in optimizing some low-level critical time code, but there is absolutely no reason to use it in PHP, and I still have to see the first example where goto will be better than any other solution.

In your case, recursion might be the best solution.

-4
source

All Articles