Download the PHP package m3m0r7/php-deferrable without Composer
On this page you can find all versions of the php package m3m0r7/php-deferrable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-deferrable
PHP-Deferrable - Simple and Powerful deferrable run code library
The PHP-Deferrable
is a simple and powerful deferrable run code library.
This library like Golang.
This library is very simple because this is not depending other libraries.
Documents
- English (Current)
- 日本語
Install
Use composer:
Issues to date
Go has a defer, and you can execute the contents of the defer before returning.
However, although PHP does not have a defer, it is possible to achieve defer using try-finally
or destructor destruction timing.
This has some problems: the post-processing code can be cumbersome, and if the try
syntax gets too long, you won't know what to do.
And you will suffer from unnecessary indentation.
php-deferrable
solves all of these problems by providing very simple functions and classes to solve the problem.
Quick Start
It will show as below:
Deferrable function
You can pass a function into the deferrable.
It will show as below:
Deferrable function can be return a value.
It will show as below:
Deferrable can manipulate resource context.
defer
can be passed any parameters and it will copy based the context.
It will show as below:
And it can be changed the parameter value in defer
function with reference.
It will show as below:
Exception of defer
Normally, php-deferrable is designed so that even if an exception is thrown in the defer, the processing of the stacked defer continues. This is to resolve the inconsistency that Go has no exceptions, but PHP does.
In the case of the above example, all exceptions are combined and returned as MergedDeferringException
.
However, you may want to stop if an exception occurs. Of course, such means are also available. If an exception occurs, there are two ways to suspend defer processing.
The first uses DeferBailableScope :: of
to return the current deferrable scope itself if an exception occurs.
or to use in class:
In this case, FirstException
is thrown as an exception to the outer scope. The reason FirstException
is thrown is
The defer process pops the stack. In other words, the process is started from the last registered defer.
Also, in contrast to DeferBailableScope
, if you want to explicitly specify an exception that can be continued, useDeferContinuableScope
.
The second is to throw an exception that inherits from DeferBailableExceptionInterface
.
If you inherit from this interface, stop merging exceptions at that point and return only those inherited exceptions.
In the above case, a SecondException
is thrown.
In the case of Defer :: createContext
, it can be controlled by passing the scope type as the first argument.
In the above case, FirstException
is thrown as an exception.
Context Manipulator
The context manipulator is very simple deferrable functions manipulator.
You can take possible to decreasing memory usage with using it.
It is not required wrapping with deferrable
function for you wanting to deferring a class.
License
MIT