Features
The library's main features
This version of the library is no longer maintained. Please consider upgrading to the latest release
Resolve class names
All class names referenced by a closure are automatically resolved at serialization time, allowing you to write a cleaner code.
use Some\Namespace\SomeClass;
use Some\Namespace as X;
use Other\Namespace\SomeClass as OtherClass;
use Some\Namespace\CaseInsensitiveClass;
$closure = function(SomeClass $a, OtherClass $b, X\SomeClass $c){
return [
new CASEInsenitiveCLASS(),
new X\SomeClass(),
new SomeClass(),
new OtherClass(),
];
};
//Class names are resolved
$closure = unserialize(serialize(new SerializableClosure($closure)))->getClosure();
Resolve function and constant names
The library also supports referencing functions and constants.
use function Some\Namespace\f;
use function Other\Namespace\f as a;
use function Some\Namespace\CaseInsensitive;
use const Some\Namespace\C;
use const Other\Namespace\C as U;
$closure = function(){
caseInsensitive();
f(U);
a(C);
};
//Names are resolved
$closure = unserialize(serialize(new SerializableClosure($closure)))->getClosure();
PHP 7.0 support
Opis Closure fully support the new PHP 7 syntax for use
statements.
use Some\Namespace{
Class1,
SubNamesace\Class2,
Class3 as T
};
use function OtherNS{
f1,
Sub\f2 as function2
}
$closure = function(Class1 $a, Class2 $b, T $c){
f1();
function2();
};
//Names are resolved
$closure = unserialize(serialize(new SerializableClosure($closure)))->getClosure();
Type hints are supported as well
use Some\Namespace as N;
$closure = function(string $a, int $b = null, $c): N\SomeClass{
return $c instanceof self;
}
//Names are resolved
$closure = unserialize(serialize(new SerializableClosure($closure)))->getClosure();
Resolve closure’s scope and $this object
Opis Closure automatically detects when the scope and the $this
object of a closure needs to be serialized
in order for the closure to work properly after deserialization.
use Opis\Closure\SerialiazbleClosure;
class A
{
function a()
{
// scope & $this are not serialized
serialize(new SerializableClosure(function(){
echo 123;
}));
}
function b()
{
// scope is serialized
serialize(new SerializableClosure(function(){
echo self::class;
}));
}
function c()
{
// scope & $this are serialized
serialize(new SerializableClosure(function(){
$this->a();
}));
}
}
You can force the serialization of the $this
object by passing true
as the second argument to the SerialiazbleClosure
constructor
or to the SerialiazbleClosure::from
method.
use Opis\Closure\SerialiazbleClosure;
class A
{
function d()
{
// scope & $this are serialized (forced)
serialize(new SerializableClosure(function(){
echo 123
}, true));
// scope & $this are serialized (forced)
serialize(SerializableClosure::from(function(){
echo 123
}, true));
}
}