Download the PHP package henzeb/closure without Composer
On this page you can find all versions of the php package henzeb/closure. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package closure
Closure
In PHP, whe have Closure::fromCallable()
. Sadly, it does not work
with FQCN strings.
This package ships a function that extends fromCallable allowing FQCN strings of invokable classes to become closures.
Installation
Just install with the following command.
usage
``
Resolving
Sometimes you may need to tell closure
how to resolve your callable
``
Note: While closure
may throw a TypeError on creation, resolving of
FQCN happens on actually calling the newly created closure. Resolving
happens only the first time. Except when the invokable method returns
a Closure, then the resolving takes place first to return this closure.
Binding
Closures are adorable hacky little creatures due to their ability
to bind and change scopes. If you ever have that need
you can use bind
.
``
To use a FCQN or invokable class with binding, the __invoke
function must
return a Closure. Please note the return type! Without, it wil fail.
``
``
Bind will under the hood use closure
to get a closure, and then
binds the new this
and scope to it. It then returns a Closure you can use.
Calling
You can also directly call a (binded) closure using call
.
``
Invoking different methods
In some cases you'd like to wrap a non-callable FQCN or class.
Each function accepts a parameter named invoke
.
``
``
invokable
To test an object is invokable, and thus can become a closure. The function accepts any value.
``
Wrapping
Sometimes you may expect a boolean or a callable. using closure
,
This would fail. Using wrap
, anything that's not invokable, will
be wrapped inside a closure.
``
Bindings
In some cases you might want to know the current binding of a closure or invokable.
``
accessing static variables
When you have closures that uses the use
clause or when you
use static variables inside your closure you can access them
with the following:
``
debug info
You can use var_dump
to print a list of all variables.
``
This will return an array with the current scope
,
the current this
and any static variables associated with it.
Testing this package
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The GNU AGPLv. Please see License File for more information.