3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( 1, 2, 3 ); $maxRetries = 5; class RetryIterator { protected static $_currentRetries = 0; public static function iterate($element) { var_dump($element); if ($element == 2) { throw new Exception('Something went wrong'); } } public static function handleException($e, $array) { var_dump('Exception handled'); } public static function retry(&$array, $maxRetries, $iterateCallback, $handleCallback) { while (reset($array)) { $key = key($array); try { call_user_func($iterateCallback, ($array[$key])); unset($array[$key]); } catch (Exception $e) { static::$_currentRetries++; if (static::$_currentRetries <= $maxRetries) { call_user_func($handleCallback, $e, $array); } else { throw new Exception('maxRetries reached'); } } } } } RetryIterator::retry($arr, 5, 'RetryIterator::iterate', 'RetryIterator::handleException');

preferences:
44.21 ms | 402 KiB | 5 Q