Download the PHP package cgtag/php-disposable without Composer

On this page you can find all versions of the php package cgtag/php-disposable. 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 php-disposable

php-disposable

License Build Status codecov Total Downloads Latest Stable Version

A tiny library that adds the disposable pattern to PHP

Requirements

PHP 7.1 or above (at least PHP 7.1.0 for now)

Install

You can easily install this library using Composer with the following command:

Documentation

The basic instructions for using this library is implementing the IDisposable interface and declaring the dispose() method that implements all the resource cleanup logic.

The following example shows a simple implementation of the basic pattern.

Now you can use the global using function to dispose of the object when you're finished with it.

Here's an example of the using function:

That might look simple but once you start following the disposable pattern memory leaks are going to be a thing of the past.

dispose()

Make sure to propagate calls to dispose() to inherited classes. You can do this by overriding the dispose() method and making sure to call parent::dispose().

Note: dispose() will only ever be called once. It will be the last method executed on a class before __destruct() is called. You don't have to worry about properties being used afterwards as dispose() is called.

The IDisposable Interface

This is the primary interface used by the library. In PHP the garbage collector automatically releases memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when the garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as file handles, images and streams.

Use the dispose() method if this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.

Global using() Function

Provides a convenience function that ensures the correct use of IDisposable objects. The following example shows how to use the global using() function.

The using function ensures that dispose() is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling dispose() in the finally block; in fact, this is how the using function is written. The code example earlier could be written as the following example:

You can instantiate the resource object and then pass the variable to the using function, but this is not a best practice. In this case, the object remains in scope after control leaves the using block even though it will probably no longer have access to its unmanaged resources. In other words, it will no longer be fully initialized. If you try to use the object outside the using callback, you risk causing an exception to be thrown. For this reason, it is generally better to instantiate the object as an argument passed to using and limit its scope to the using callback.

Note: using should not be confused with use

The DisposableTrait

The DisposeTrait allows an object to automatically dispose of public properties. When an object implements the IDisposable interface and uses the DisposeTrait when that object is disposed then all the public properties that reference an IDisposable object are also disposed of and unset. The trait also deeply walks all public arrays disposing of any objects that are in those arrays.

Here is an example object that uses the ConfigReader from above examples:

Privates And Memory Leaks

The DisposeTrait can not dispose of private properties, but will throw an exception when a private property references an object that implements the IDisposable interface. The trait does this because it found a possible memory leak. Since the object is using the trait and private properties are not supported it means that the property might not be disposed.

To resolve this conflict make the property public or implement the dispose() method on the object.

License

php-disposable is licensed under the MIT License - see the LICENSE file for details


All versions of php-disposable with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
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 cgtag/php-disposable contains the following files

Loading the files please wait ....