3v4l.org

run code in 300+ PHP versions simultaneously
<?php class RetriesExceededException extends \Exception {} class Retry { static public function withDelay(callable $fn, $retries, $delay) { return self::execute($fn, $retries, $delay); } static public function immediately(callable $fn, $retries) { return self::execute($fn, $retries); } static private function execute(callable $fn, $retries, $delay = null) { while ($retries) { $retries--; try { return $fn(); } catch (\Exception $e) { echo $retries; if ($retries && $delay) { //do delay } } } throw new RetriesExceededException(); } } class Test { public function doSomething() { throw new \Exception(); } } $test = new Test(); try { Retry::immediately(function() use ($test) { $test->doSomething(); }, 5); } catch (\Exception $e) { var_dump($e); } //Retry::withDelay($fn, $retries, $delay); //Retry::immediately($fn, $retries);

preferences:
49.64 ms | 402 KiB | 5 Q