3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( 1, 2, 3 ); $maxRetries = 5; class RetryCatch { 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)) { $useIterateParams = $iterateParams; array_unshift($useIterateParams, $array[$key]); call_user_func_array($iterateCallback, $useIterateParams); } else { call_user_func($iterateCallback, ($array[$key])); } } else { if (is_array($iterateParams) && !empty($iterateParams)) { $useIterateParams = $iterateParams; array_unshift($useIterateParams, $array[$key]); // var_dump($iterateParams); die(); call_user_func_array(array($iterateCallbackObject, $iterateCallback), $useIterateParams); } 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)) { $useHandleParams = $handleParams; array_unshift($useHandleParams, $e, $array); call_user_func_array($handleCallback, $useHandleParams); } else { call_user_func($handleCallback, $e, $array); } } else { if (is_array($handleParams) && !empty($handleParams)) { $useHandleParams = $handleParams; array_unshift($useHandleParams, $e, $array); call_user_func_array(array($handleCallbackObject, $handleCallback), $useHandleParams); } else { $handleCallbackObject->$handleCallback($e, $array); } } } else { throw new Exception('RetryCatch::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 { RetryCatch::retry($arr, 5, 'RetryCatch::iterate', 'RetryCatch::handleException'); } catch (Exception $e) { } $arr = array(1, false, 2, 3); $foo = new Foo(); try { RetryCatch::retry($arr, 5, 'bar', 'handle', null, null, $foo, $foo); } catch (Exception $e) { } $arr = array(1, false, 2, 3); $foo = new Foo(); try { RetryCatch::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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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) array(4) { [0]=> int(1) [1]=> int(1) [2]=> int(2) [3]=> int(3) } int(1) array(4) { [0]=> bool(false) [1]=> int(1) [2]=> int(2) [3]=> int(3) } bool(false) array(4) { [0]=> int(2) [1]=> int(1) [2]=> int(2) [3]=> int(3) } int(2)
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> int(5) [2]=> string(19) "RetryCatch::iterate" [3]=> string(27) "RetryCatch::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) array(4) { [0]=> int(1) [1]=> int(1) [2]=> int(2) [3]=> int(3) } int(1) array(4) { [0]=> bool(false) [1]=> int(1) [2]=> int(2) [3]=> int(3) } bool(false) array(4) { [0]=> int(2) [1]=> int(1) [2]=> int(2) [3]=> int(3) } int(2)
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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(19) "RetryCatch::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(19) "RetryCatch::iterate" [3]=> &string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(19) "RetryCatch::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(19) "RetryCatch::iterate" [3]=> &string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(19) "RetryCatch::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(19) "RetryCatch::iterate" [3]=> &string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(19) "RetryCatch::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(19) "RetryCatch::iterate" [3]=> &string(27) "RetryCatch::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/meNKI" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(7) "iterate" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(1) { [0]=> int(2) } } [1]=> array(4) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(34) ["function"]=> string(14) "call_user_func" ["args"]=> array(2) { [0]=> string(19) "RetryCatch::iterate" [1]=> int(2) } } [2]=> array(6) { ["file"]=> string(9) "/in/meNKI" ["line"]=> int(91) ["function"]=> string(5) "retry" ["class"]=> string(10) "RetryCatch" ["type"]=> string(2) "::" ["args"]=> array(4) { [0]=> &array(2) { [1]=> int(2) [2]=> int(3) } [1]=> &int(5) [2]=> &string(19) "RetryCatch::iterate" [3]=> &string(27) "RetryCatch::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) array(4) { [0]=> int(1) [1]=> int(1) [2]=> int(2) [3]=> int(3) } int(1) array(4) { [0]=> bool(false) [1]=> int(1) [2]=> int(2) [3]=> int(3) } bool(false) array(4) { [0]=> int(2) [1]=> int(1) [2]=> int(2) [3]=> int(3) } int(2)

preferences:
259.48 ms | 422 KiB | 298 Q