Skip to content

Instantly share code, notes, and snippets.

@Sairahcaz
Last active January 31, 2025 12:47
Show Gist options
  • Save Sairahcaz/4116f87c3707ffbae607c3052f238c79 to your computer and use it in GitHub Desktop.
Save Sairahcaz/4116f87c3707ffbae607c3052f238c79 to your computer and use it in GitHub Desktop.
<?php
// if you wanna use defer in a package where it may only be optional
// or you don't know or want to mess with if defer is already supported
// also swoole compatible if you get this error:
// Symfony\Component\ErrorHandler\Error\FatalError: Uncaught Swoole\Error: API must be called in the coroutine
if (! function_exists('safeDefer')) {
/**
* Safe Defer with fallback and swoole compatible.
*/
function safeDefer(?callable $callback = null, ?string $name = null, bool $always = false): void
{
if (function_exists('Illuminate\Support\defer')) {
Illuminate\Support\defer(...func_get_args());
} else {
$callback();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment