This class MUST be used to manage deferred callbacks that SHALL be executed at the end of a scope.
Description
It provides mechanisms to register, execute, and report errors for callbacks in a LIFO order.
Interfaces
Properties
The global error reporter instance. This property MAY be set to customize error reporting.
Stack of deferred callbacks and their arguments.
Methods
Constructs a new Defer instance and optionally registers an initial callback.
Executes all registered callbacks when the object is destroyed.
Registers a callback to be executed later.
Returns the number of registered callbacks.
Registers a callback to be executed later.
Determines if there are no registered callbacks.
Sets the global error reporter for all Defer instances.
Executes all registered callbacks, reporting exceptions as needed.
Gets the configured error reporter or instantiates a default one.
The global error reporter instance. This property MAY be set to customize error reporting.
private
static
ErrorReporterInterface|null
$errorReporter
=
null
Stack of deferred callbacks and their arguments.
private
array<int, array{0: callable, 1: array}>
$stack
=
[]
Description
the stack of callbacks
Constructs a new Defer instance and optionally registers an initial callback.
public
__construct([
callable|null
$callback = null],
mixed
...$arguments) : mixed
Parameters
$callback
:
callable|null
=
null
Description
the initial callback to register (optional)
$arguments
:
mixed
Description
Arguments for the callback.
Executes all registered callbacks when the object is destroyed.
public
__destruct() : mixed
Registers a callback to be executed later.
public
__invoke(
callable
$callback,
mixed
...$arguments) : void
Description
This method MUST add the callback to the stack. It MAY be called multiple times.
Parameters
$callback
:
callable
Description
the callback to register
$arguments
:
mixed
Description
Arguments for the callback.
Returns the number of registered callbacks.
public
count() : int
Return values
Description
the number of callbacks
Registers a callback to be executed later.
public
defer(
callable
$callback,
mixed
...$args) : void
Description
This method MUST add the callback to the stack. It MAY be called multiple times.
Parameters
$callback
:
callable
Description
the callback to register
$args
:
mixed
Description
Arguments for the callback.
Determines if there are no registered callbacks.
public
isEmpty() : bool
Return values
Description
true if no callbacks are registered; otherwise, false
Sets the global error reporter for all Defer instances.
public
static
setErrorReporter(ErrorReporterInterface|null
$reporter) : void
Parameters
Description
the error reporter to use
Executes all registered callbacks, reporting exceptions as needed.
private
flush() : void
Description
This method MUST execute callbacks in LIFO order. If a callback throws, the error MUST be reported.
Gets the configured error reporter or instantiates a default one.
private
getErrorReporter() : ErrorReporterInterface
Return values
Description
the error reporter instance