Defer

Class
implements DeferInterface
Final: Yes

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.

Table of Contents

Interfaces

DeferInterface

This interface MUST be implemented by any class that manages deferred callbacks.

Properties

$errorReporter

The global error reporter instance. This property MAY be set to customize error reporting.

 : ErrorReporterInterface|null
$stack

Stack of deferred callbacks and their arguments.

 : array<int, array{0: callable, 1: array}>

Methods

__construct()

Constructs a new Defer instance and optionally registers an initial callback.

 : mixed
__destruct()

Executes all registered callbacks when the object is destroyed.

 : mixed
__invoke()

Registers a callback to be executed later.

 : void
count()

Returns the number of registered callbacks.

 : int
defer()

Registers a callback to be executed later.

 : void
isEmpty()

Determines if there are no registered callbacks.

 : bool
setErrorReporter()

Sets the global error reporter for all Defer instances.

 : void
flush()

Executes all registered callbacks, reporting exceptions as needed.

 : void
getErrorReporter()

Gets the configured error reporter or instantiates a default one.

 : ErrorReporterInterface
Properties

$errorReporter

Private Static

The global error reporter instance. This property MAY be set to customize error reporting.

private static ErrorReporterInterface|null $errorReporter = null

$stack

Private

Stack of deferred callbacks and their arguments.

private array<int, array{0: callable, 1: array}> $stack = []

Description

the stack of callbacks

Methods

__construct()

Public

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.

__destruct()

Public

Executes all registered callbacks when the object is destroyed.

public __destruct() : mixed

__invoke()

Public

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.

count()

Public

Returns the number of registered callbacks.

public count() : int
Return values
int

Description

the number of callbacks

defer()

Public

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.

isEmpty()

Public

Determines if there are no registered callbacks.

public isEmpty() : bool
Return values
bool

Description

true if no callbacks are registered; otherwise, false

setErrorReporter()

Public Static

Sets the global error reporter for all Defer instances.

public static setErrorReporter(ErrorReporterInterface|null  $reporter) : void
Parameters
$reporter : ErrorReporterInterface|null

Description

the error reporter to use

flush()

Private

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.