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