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); var_dump(func_get_args()); if ($element == 2) { throw new Exception('Something went wrong'); } } public static function handleException($e, $array) { var_dump(func_get_args()); var_dump('Exception handled'); } public static function retry(&$array, $maxRetries, $iterateCallback, $handleCallback, $iterateParams = null, $handleParams = null, $iterateCallbackObject = null, $handleCallbackObject = null) { while (!empty($array)) { reset($array); $key = key($array); try { if (!is_object($iterateCallbackObject)) { if (is_array($iterateParams) && !empty($iterateParams)) { array_unshift($handleParams, $array[$key]); call_user_func_array($iterateCallback, $iterateParams); } else { call_user_func($iterateCallback, ($array[$key])); } } else { if (is_array($iterateParams) && !empty($iterateParams)) { array_unshift($handleParams, $array[$key]); call_user_func_array(array($iterateCallbackObject, $iterateCallback), $iterateParams); } else { $iterateCallbackObject->$iterateCallback($array[$key]); } } unset($array[$key]); } catch (Exception $e) { static::$_currentRetries++; if (static::$_currentRetries <= $maxRetries) { if (!is_object($handleCallbackObject)) { if (is_array($handleParams) && !empty($handleParams)) { array_unshift($handleParams, $e, $array); call_user_func_array($handleCallback, $handleParams); } else { call_user_func($handleCallback, $e, $array); } } else { if (is_array($handleParams) && !empty($handleParams)) { array_unshift($handleParams, $e, $array); call_user_func_array(array($handleCallbackObject, $handleCallback), $handleParams); } else { $handleCallbackObject->$handleCallback($e, $array); } } } else { throw new Exception('maxRetries reached'); } } } } } class Foo { public function bar($el, $a = null, $b = null, $c = null) { var_dump(func_get_args()); var_dump($el); if ($el == 2) { throw new Exception('Exception'); } } public function handle($e, $arr, $a = null, $b = null, $c = null) { var_dump(func_get_args()); var_dump('Exception handled'); } } try { RetryIterator::retry($arr, 5, 'RetryIterator::iterate', 'RetryIterator::handleException'); } catch (Exception $e) { } $arr = array(1, false, 2, 3); $foo = new Foo(); try { RetryIterator::retry($arr, 5, 'bar', 'handle', null, null, $foo, $foo); } catch (Exception $e) { } $arr = array(1, false, 2, 3); $foo = new Foo(); var_dump('objects'); try { RetryIterator::retry($arr, 5, 'bar', 'handle', array(1, 2, 3), array(1, 2, 3), $foo, $foo); } catch (Exception $e) { }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
int(1) array(1) { [0]=> int(1) } int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#2 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#2 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(1) { [0]=> int(1) } int(1) array(1) { [0]=> bool(false) } bool(false) array(1) { [0]=> int(2) } int(2) string(7) "objects" array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1)
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33
int(1) array(1) { [0]=> int(1) } int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#2 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#2 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(22) "RetryIterator::iterate" [3]=> string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(1) { [0]=> int(1) } int(1) array(1) { [0]=> bool(false) } bool(false) array(1) { [0]=> int(2) } int(2) string(7) "objects" array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1)
Output for 5.3.7 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
int(1) array(1) { [0]=> int(1) } int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(22) "RetryIterator::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(22) "RetryIterator::iterate" [3]=> &string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#2 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(22) "RetryIterator::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(22) "RetryIterator::iterate" [3]=> &string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(22) "RetryIterator::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(22) "RetryIterator::iterate" [3]=> &string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#2 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(22) "RetryIterator::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(22) "RetryIterator::iterate" [3]=> &string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(2) { [0]=> object(Exception)#1 (7) { ["message":protected]=> string(20) "Something went wrong" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/VutDj" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(33) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(22) "RetryIterator::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/VutDj" ["line"]=> int(86) ["function"]=> string(5) "retry" ["class"]=> string(13) "RetryIterator" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(22) "RetryIterator::iterate" [3]=> &string(30) "RetryIterator::handleException" } } } ["previous":"Exception":private]=> NULL } [1]=> array(2) { [1]=> int(2) [2]=> int(3) } } string(17) "Exception handled" int(2) array(1) { [0]=> int(2) } array(1) { [0]=> int(1) } int(1) array(1) { [0]=> bool(false) } bool(false) array(1) { [0]=> int(2) } int(2) string(7) "objects" array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1) array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } int(1)

preferences:
257.51 ms | 422 KiB | 250 Q