Enforced disposal of objects in PHP. ๐Ÿ˜

Related tags

Miscellaneous using
Overview

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.

$file->contents(), ]); }); // The `$resource` property is no-longer a valid stream, since we closed // the handle in the `dispose` method. var_dump($file->resource); ">
// 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.

You might also like...
A PHP 7 value objects helper library.

valueobjects Requirements Requires PHP = 7.1 Installation Through Composer, obviously: composer require funeralzone/valueobjects Extensions This lib

php String Objects Chains like length,forEach,filter,replace,repalcAll much More.... Module

php String Objects Chains like length,forEach,filter,replace,repalcAll much More.... Module

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific.

QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific. It is part of the QuidPHP package and can also be used standalone.

PHP library with basic objects and more for working with Facebook/Metas Conversions API

PHP library with basic objects and more for working with Facebook/Metas Conversions API Installation The easiest way to install this library is by ins

A set of classes to create and manipulate HTML objects abstractions

HTMLObject HTMLObject is a set of classes to create and manipulate HTML objects abstractions. Static calls to the classes echo Element::p('text')-cla

Share value objects that contain the same primitive value as a singleton

sharable-value-objects Share value objects that contain the same primitive value as a singleton. Singletons are kept under WeakReference objects. Inst

DeepCopy - Create deep copies (clones) of your objects
DeepCopy - Create deep copies (clones) of your objects

DeepCopy DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph. Table of Contents H

Releases(v0.2.0)
Owner
Ryan Chandler
Ryan Chandler
Attributes to define PHP language extensions (to be enforced by static analysis)

PHP Language Extensions (currently in BETA) This library provides attributes for extending the PHP language (e.g. adding package visibility). The inte

Dave Liddament 70 Dec 19, 2022
Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Super Simple DTO Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Mohammed Manssour 8 Jun 8, 2023
PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects.

?? Yell PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects. Requirement

Zeeshan Ahmad 20 Dec 8, 2018
Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.DeepEqual().

Deeper Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.De

Joubert RedRat 4 Feb 12, 2022
PHP with PDO (PHP Data Objects) Quickstart

PHP with PDO (PHP Data Objects) Quickstart This repository contains a simple web application that demonstrates how to quickly connect to and communica

Developer Code Central 3 Oct 20, 2022
:date: The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects

sabre/vobject The VObject library allows you to easily parse and manipulate iCalendar and vCard objects using PHP. The goal of the VObject library is

sabre.io 532 Dec 25, 2022
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

zhixin 2 Sep 9, 2022
SAPI request and response objects for PHP 8.1

Sapien This package provides server API (SAPI) request and response objects for PHP 8.1: Sapien\Request, composed of readonly copies of PHP supergloba

null 37 Jan 3, 2023
Samsui is a factory library for building PHP objects useful for setting up test data in your applications.

#Samsui Samsui is a factory library for building PHP objects useful for setting up test data in your applications. It is mainly inspired by Rosie for

Sam Yong 31 Nov 11, 2020
Collection of value objects that represent the types of the PHP type system

sebastian/type Collection of value objects that represent the types of the PHP type system. Installation You can add this library as a local, per-proj

Sebastian Bergmann 1.1k Dec 29, 2022