\SuperClosureSerializableClosure

This class acts as a wrapper for a closure, and allows it to be serialized.

With the combined power of the Reflection API, code parsing, and the infamous eval() function, you can serialize a closure, unserialize it somewhere else (even a different PHP process), and execute it.

Summary

Methods
Properties
Constants
__construct()
getClosure()
__invoke()
bindTo()
serialize()
unserialize()
__debugInfo()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$closure
$serializer
$data
N/A

Properties

$closure

$closure : \Closure

The closure being wrapped for serialization.

Type

\Closure

$data

$data : array

The data from unserialization.

Type

array

Methods

__construct()

__construct(\Closure  $closure, \SuperClosure\SerializerInterface|null  $serializer = null) 

Create a new serializable closure instance.

Parameters

\Closure $closure
\SuperClosure\SerializerInterface|null $serializer

getClosure()

getClosure() : \Closure

Return the original closure object.

Returns

\Closure

__invoke()

__invoke() : mixed

Delegates the closure invocation to the actual closure object.

Important Notes:

  • ReflectionFunction::invokeArgs() should not be used here, because it does not work with closure bindings.
  • Args passed-by-reference lose their references when proxied through __invoke(). This is an unfortunate, but understandable, limitation of PHP that will probably never change.

Returns

mixed

bindTo()

bindTo(mixed  $newthis, mixed  $newscope = 'static') : \SuperClosure\SerializableClosure

Clones the SerializableClosure with a new bound object and class scope.

The method is essentially a wrapped proxy to the Closure::bindTo method.

Parameters

mixed $newthis

The object to which the closure should be bound, or NULL for the closure to be unbound.

mixed $newscope

The class scope to which the closure is to be associated, or 'static' to keep the current one. If an object is given, the type of the object will be used instead. This determines the visibility of protected and private methods of the bound object.

Returns

\SuperClosure\SerializableClosure

serialize()

serialize() : string|null

Serializes the code, context, and binding of the closure.

Returns

string|null

unserialize()

unserialize(string  $serialized) 

Unserializes the closure.

Unserializes the closure's data and recreates the closure using a simulation of its original context. The used variables (context) are extracted into a fresh scope prior to redefining the closure. The closure is also rebound to its former object and scope.

Parameters

string $serialized

Throws

\SuperClosure\Exception\ClosureUnserializationException

__debugInfo()

__debugInfo() : array

Returns closure data for `var_dump()`.

Returns

array