Skip to content

Instantly share code, notes, and snippets.

@phcorp
Created June 10, 2020 15:59
Show Gist options
  • Save phcorp/5b6c51cda2dfdf49083e7ebccd5815ac to your computer and use it in GitHub Desktop.
Save phcorp/5b6c51cda2dfdf49083e7ebccd5815ac to your computer and use it in GitHub Desktop.
Simple php retry
<?php
declare(strict_types=1);
namespace Utils;
/**
* @param callable $function Function to call
* @param int $times Maximum number of times the function is called
*
* @throws \Exception
*
* @return mixed $function return
*/
function retry(callable $function, int $times = 3)
{
do {
--$times;
try {
return $function();
} catch (\Exception $exception) {
if (!$times) {
throw $exception;
}
}
} while ($times);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment