Download the PHP package fast-forward/defer without Composer
On this page you can find all versions of the php package fast-forward/defer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fast-forward/defer
More information about fast-forward/defer
Files in fast-forward/defer
Package defer
Short Description Fast Forward Deferred Callbacks utility classes
License MIT
Homepage https://php-fast-forward.github.io/defer/
Informations about the package defer
FastForward Defer
A minimal utility that brings defer-style execution (similar to Go) to PHP using object scope and destructors. It allows you to register callbacks that will run automatically at the end of a scope, in LIFO order (Last-In, First-Out).
Core Concept
A Defer instance represents a scope-bound execution stack.
- Register callbacks using $defer(...) or $defer->defer(...)
- Callbacks execute automatically when the object goes out of scope
- Execution is always LIFO (stack behavior)
- Execution is guaranteed during exception unwinding
- Errors inside callbacks are captured and reported, without interrupting the chain
Basic Usage
Output
Using Arguments
Deferred callbacks receive exactly the arguments you pass.
Output
⚠️ Execution Order Matters
Deferred callbacks execute in reverse order of registration.
Execution order:
Always register in reverse of the desired execution order.
Exception Safety
Deferred callbacks always run, even if an exception occurs.
Output
Nested Scopes
Each Defer instance is isolated.
Output
Helper Functions
defer()
Creates a new Defer instance.
scope()
Runs a block with an isolated defer scope.
using()
Structured resource management (acquire → use → cleanup).
Error Handling
Deferred callbacks never break execution flow.
- If a callback throws, execution continues
- Errors are forwarded to an ErrorReporter
- Default behavior uses error_log()
Custom Error Reporter
Composite Reporter
PSR Integration
PSR-3 Logger
PSR-14 Event Dispatcher
This allows multiple listeners (logging, metrics, tracing, etc.).
HTTP Middleware (PSR-15)
You can bind a Defer instance to a request lifecycle.
The middleware:
- creates a Defer per request
- injects it into request attributes
- ensures execution at the end of the request
Accessing Defer in Handlers
Execution Model
Within a single scope
- Last registered → runs first
Across nested scopes
- Inner scope resolves before outer
Design Principles
- Deterministic cleanup
- Minimal API
- No manual flush
- Failure isolation
- Extensible reporting
Notes
- Execution is triggered by __destruct()
- Do not share instances across scopes
- Prefer short-lived instances
- Avoid long-lived/global usage
When to Use
- resource cleanup
- file handling
- locks
- temporary state
- exception-safe teardown
When NOT to Use
- long-lived lifecycle management
- orchestration logic
- cases requiring explicit execution timing
Summary
Defer provides a strict and predictable cleanup model:
- automatic execution at scope end
- LIFO ordering
- safe failure handling
- pluggable reporting
- PSR-friendly integrations
It is intentionally small, deterministic, and constrained.
Contributing
See CONTRIBUTING.md for changelog fragments, local checks and the release-review flow.
Consumer agent skill
The package includes fast-forward-defer for application integration.
It supports fast-forward/defer:^1.1; inspect the installed version.
See skill installation and lifecycle for
optional project synchronization with llm/skills ^1.15, guarded manual
project/global copies and a pinned Skills CLI alternative. Shipping files does
not install tooling or activate the skill in an agent session.