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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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:

  1. Function Scope - Executes when the defer instance goes out of scope (LIFO order)
  2. Global Scope - Executes during script shutdown (LIFO order)
  3. 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):

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

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

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

  1. Signal handling is opt-in via Defer::enableSignals() — global defers only cover normal shutdown by default
  2. Terminate defers work optimally in FastCGI environments; other environments use fallback methods
  3. Exceptions in defer callbacks are logged but do not propagate
  4. Defer stacks have size limits to prevent memory leaks (see Performance Considerations)
  5. Execution order differs by scope — plan your cleanup registration accordingly

License

MIT License - see LICENSE file for details.


All versions of defer with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package rcalicdan/defer contains the following files

Loading the files please wait ...