execute() public method

The code has to be designed in a way that it can be repeated without any side effects. When execution was successful it should notify that event by calling {@link Loop::end()}. I.e. the only side effects of the code may happen after a successful execution. If the code throws an exception it will stop repeating the execution.
public execute ( callable $code ) : mixed
$code callable The executed code block.
return mixed The return value of the executed block.
Beispiel #1
0
 /**
  * Tests that the code is executed more times.
  *
  * @test
  */
 public function testIteration()
 {
     $i = 0;
     $loop = new Loop();
     $loop->execute(function () use($loop, &$i) {
         $i++;
         if ($i > 1) {
             $loop->end();
         }
     });
     $this->assertEquals(2, $i);
 }