ryangjchandler/
using
g4t.io

Enforce the Disposal of Objects in PHP

by Paul Redmond

The using() PHP package enforces the disposal of objects in PHP

README from ryangjchandler/using Open on GitHub →

Enforced disposal of objects in PHP.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a Disposable interface and using() global function that can be used to enforce the disposal of objects.

Installation

You can install the package via composer:

composer require ryangjchandler/using

Usage

You should first implement the RyanChandler\Using\Disposable interface on your class. This contract requires an implementation of a public function dispose(): void method.

class TextFile implements Disposable
{
    private $resource;

    public function dispose(): void
    {
        $this->resource = fclose($this->resource);
    }
}

You can then use the using() helper function with your Disposable object to enforce disposal.

// This code might create a file pointer and store it on the class.
$file = new TextFile('hello.txt');

// We can then "use" the `$file` object inside of this callback. After the callback has been
// invoked, the `TextFile::dispose()` method will be called.
using($file, function (TextFile $file) {
    DB::create('messages', [
        'message' => $file->contents(),
    ]);
});

// The `$resource` property is no-longer a valid stream, since we closed
// the handle in the `dispose` method.
var_dump($file->resource);

Handling Exceptions

The using() function will wrap the invokation of your callback in a try..finally statement.

This ensures that your object is disposed of regardless of any exceptions.

Any exceptions thrown inside of your callback will still propagate up to the top-level.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

More from the ecosystem

marcreichel/
laya-php
g4t.io
3 53

LayaPHP: Self-Hosted Text Classification for PHP and Laravel

Classify text in PHP without an LLM bill: typed decisions in 100+ languages, self-hosted. Laravel-ready SDK for Laya, a Jev AI alternative.

ai classification decision-engine
marcreichel/laya-php via Laravel News
Josh-Dovey/
postcodes-laravel
g4t.io
5 9

Postcodes for Laravel: GB Postcode Lookup and Geography Data

Postcodes for Laravel adds typed GB postcode lookups, validation, geography data, distance searches, and test fakes through the GB Postcodes API.

Josh-Dovey/postcodes-laravel via Laravel News
RedberryProducts/
mailbox-for-laravel
g4t.io
4 115

Mailbox for Laravel: Preview and Test Rendered Email

Mailbox for Laravel captures outgoing mail in a local dashboard and lets you test rendered HTML, recipients, and attachments with fluent assertions.

RedberryProducts/mailbox-for-laravel via Laravel News