Download the PHP package rcalicdan/defer without Composer
On this page you can find all versions of the php package rcalicdan/defer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rcalicdan/defer
More information about rcalicdan/defer
Files in rcalicdan/defer
Package defer
Short Description Framework agnostic Deferred Execution Library for PHP
License MIT
Homepage https://github.com/rcalicdan/defer
Informations about the package defer
Defer - PHP Deferred Execution Library
A framework-agnostic PHP library that provides Go-style defer functionality for resource management and cleanup operations. Execute callbacks at different scopes: function-level, global (shutdown), or after HTTP response termination.
Installation
Requirements: PHP 8.2+
Quick Start
Core Concepts
The library provides three execution scopes:
- Function Scope - Executes when the defer instance goes out of scope (LIFO order)
- Global Scope - Executes during script shutdown (LIFO order)
- Terminate Scope - Executes after HTTP response is sent (FIFO order)
Function-Scoped Defers
Function-scoped defers execute when the DeferInstance object is destroyed (typically when leaving the function scope). They execute in LIFO (Last In, First Out) order.
Basic Usage
Method Chaining
Multiple Resources
Global Defers
Global defers execute during normal script shutdown in LIFO (Last In, First Out) order. They are guaranteed to run when the script exits naturally or encounters a fatal error.
Practical Example
Signal Handling (Opt-In)
By default, global defers only run on normal script shutdown. If you need defers to also run when the process is interrupted (e.g. Ctrl+C, SIGTERM, SIGHUP), you must explicitly opt in by calling Defer::enableSignals() early in your script.
This is intentionally disabled by default — registering signal handlers can have unexpected side effects in web contexts, test runners, and scripts that manage their own signals.
Signal support by platform (when opted in):
- Windows —
sapi_windows_set_ctrl_handler()(Ctrl+C, Ctrl+Break, window close) - Unix/Linux with pcntl —
SIGTERM,SIGINT,SIGHUP - Unix/Linux without pcntl — process monitoring, STDIN monitoring, error handler fallbacks
- All platforms —
register_shutdown_function()as the guaranteed baseline
Note: Signal handling is only meaningful in CLI. Calling
Defer::enableSignals()in a web context is a safe no-op.
Terminate Defers
Terminate defers execute after the HTTP response is sent to the client in FIFO (First In, First Out) order, allowing for background processing without impacting response time.
Note: Terminate defers work best in FastCGI environments (PHP-FPM, FastCGI) where fastcgi_finish_request() is available. Other environments use fallback methods but may not guarantee true post-response execution.
Basic Usage
Error Handling
By default, terminate defers skip execution on 4xx/5xx HTTP status codes. Use the $always parameter to force execution:
Environment Support
- FastCGI/FPM ✅ — Uses
fastcgi_finish_request()for true post-response execution - CLI — Executes after main script completion
- Development Server — Flushes output buffers before execution
- Other SAPIs — Fallback with output buffer handling
Advanced Usage
Manual Execution (Testing)
Monitoring and Debugging
Checking FastCGI Availability
Error Handling
All defer types include robust error handling. Exceptions in callbacks are logged but do not prevent remaining callbacks from executing:
Performance Considerations
- Function Scope: Limited to 50 defers per instance (oldest dropped when exceeded)
- Global Scope: Limited to 100 defers total (oldest dropped when exceeded)
- Terminate Scope: Limited to 50 defers (oldest dropped when exceeded)
- Function and Global defers execute in LIFO order
- Terminate defers execute in FIFO order
- Minimal overhead for registration and cleanup
Real-World Examples
Database Transaction with Cleanup
File Processing with Temporary Cleanup
Background Processing with Terminate
Long-Running CLI Process with Graceful Shutdown
Execution Order Summary
| Scope | Order | Triggered by |
|---|---|---|
| Function | LIFO | Instance going out of scope |
| Global | LIFO | Script shutdown (+ signals if opted in) |
| Terminate | FIFO | After HTTP response is sent |
Limitations
- Signal handling is opt-in via
Defer::enableSignals()— global defers only cover normal shutdown by default - Terminate defers work optimally in FastCGI environments; other environments use fallback methods
- Exceptions in defer callbacks are logged but do not propagate
- Defer stacks have size limits to prevent memory leaks (see Performance Considerations)
- Execution order differs by scope — plan your cleanup registration accordingly
License
MIT License - see LICENSE file for details.