2017 © Pedro PelĂĄez
 

library defer

image

liamja/defer

  • Sunday, February 18, 2018
  • by liamja
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Defer

Postpone the calling of a function or callable., (*1)

Why?

From Go by Example:, (*2)

Defer is used to ensure that a function call is performed later in a program’s execution, usually for purposes of cleanup. defer is often used where e.g. ensure and finally would be used in other languages., (*3)

Common use cases are: * Cleaning up temporary files. * Closing network connections. * Closing database connections., (*4)

Comparing defer to finally, this implementation of defer will allow us to have better control over when our deferred functions are called; we can decide when to start stacking deferred functions, and where to finally call them., (*5)

Examples

Usage

// Create an instance of Defer.
// When $defer falls out of scope, the deferred callables will be called in reverse order.
$defer = new Defer;

// Push your deferred tasks to the $defer object.
$defer->push(function () {
    echo "I'm echoed last!";
});

// As a convenience, you can also call $defer as a function
$defer(function () {
    echo "I'm echoed second!";
});

echo "I'm called first!";

Closing Resources

Defer can be used for ensuring the closing of open files:, (*6)

$fp = fopen('/tmp/file', 'w');

$defer(function () use ($fp) {
   fclose($fp);
});

fwrite($fp, 'Some temporary data.');

The Versions

18/02 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Liam Anderson