Save me from raptor death - is there any better way to handle this kind of structure?
while(condition) {
$this->phase1();
$this->phase2();
$this->phase3();
$this->phase4();
}
Throughout either one of those methods, the condition could be met. IMMEDIATELY after the condition is met, the loop MUST exit. If I could call break;
inside of phase2();
for example, I wouldn't need a goto statement (but of course, that would throw an error).
Source: Tips4all
Return a boolean to execute each stage until successful.
ReplyDeletewhile (condition) {
if ($this->phase1() || $this->phase2() || $this->phase3() || $this->phase4()) {
// Success!
}
}
Or you could use the State pattern!
ReplyDeleteIn summary, instead of having a goto statement, change the internal state of $this such that methods phase1, phase2, phase3 and phase4 have no effect and are empty functions. Since they will be empty functions, you'll fly right through 'em and exit the loop!
You'll probably also need a pinch of events or the Observer pattern, to know when to change state.
Return a boolean from the different phases. I would return false if it didn't succeed, then check for that and break.
ReplyDeleteThrow an exception... sounds exceptional...
ReplyDeletetry {
while(condition) {
$this->phase1();
$this->phase2();
$this->phase3();
$this->phase4();
}
} catch (Exception $e) { }
One way might be to use if() statements to check for a return value from each phase*() and break the while loop.
ReplyDeleteSomething like this:
while (condition) {
if ($this->phase1()) { break; }
}
How about wrapping each of the phases in an if:
ReplyDeletewhile(condition)
{
if(condition)
{
$this->phase1();
}
if(condition)
{
$this->phase2();
}
if(condition)
{
$this->phase3();
}
if(condition)
{
$this->phase4();
}
}
Of course, this could probably be made more compact with a bit of planning and a loop.
Please don't use a goto statement, think to the coder who will herit your code base
ReplyDeleteif your condition it's not too much of overhead, you could
while(*condition*){
$this->phase1();
if (*condition*){
$this->phase2();
}
...
}
$phases=array('phase1','phase2','phase3','phase4');
ReplyDeleteforeach($phases as $phase){
$this->$phase();
if(condition)break;
}
Also , you can use exceptions if you want to break out from inside of the function.