final class Closure (View source)

Class used to represent anonymous functions.

Anonymous functions, implemented in PHP 5.3, yield objects of this type. This fact used to be considered an implementation detail, but it can now be relied upon. Starting with PHP 5.4, this class has methods that allow further control of the anonymous function after it has been created.

Besides the methods listed here, this class also has an __invoke method. This is for consistency with other classes that implement calling magic, as this method is not used for calling the function.

Methods

mixed
__invoke(mixed ...$_)

This is for consistency with other classes that implement calling magic, as this method is not used for calling the function.

Closure|null
bindTo(object|null $newThis, object|string|null $newScope = 'static')

Duplicates the closure with a new bound object and class scope

static Closure|null
bind(Closure $closure, object|null $newThis, object|string|null $newScope = 'static')

This method is a static version of Closure::bindTo().

mixed
call(object $newThis, mixed ...$args)

Temporarily binds the closure to newthis, and calls it with any given parameters.

static Closure
fromCallable(callable $callback)

No description

Details

mixed __invoke(mixed ...$_)

This is for consistency with other classes that implement calling magic, as this method is not used for calling the function.

Parameters

mixed ...$_ [optional]

Return Value

mixed

Closure|null bindTo(object|null $newThis, object|string|null $newScope = 'static')

Duplicates the closure with a new bound object and class scope

Parameters

object|null $newThis

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

object|string|null $newScope

The class scope to which associate 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.

Return Value

Closure|null

Returns the newly created Closure object or null on failure

static Closure|null bind(Closure $closure, object|null $newThis, object|string|null $newScope = 'static')

This method is a static version of Closure::bindTo().

See the documentation of that method for more information.

Parameters

Closure $closure

The anonymous functions to bind.

object|null $newThis

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

object|string|null $newScope

The class scope to which associate 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.

Return Value

Closure|null

Returns the newly created Closure object or null on failure

mixed call(object $newThis, mixed ...$args)

Since: 7.0

Temporarily binds the closure to newthis, and calls it with any given parameters.

Parameters

object $newThis

The object to bind the closure to for the duration of the call.

mixed ...$args

[optional] Zero or more parameters, which will be given as parameters to the closure.

Return Value

mixed

static Closure fromCallable(callable $callback)

Since: 7.1

No description

Parameters

callable $callback

Return Value

Closure