php8.5
Home/ Manual/ predefined / closure/ Closure::call

Closure::call

PHP function Edit on GitHub ✎

(PHP 7, PHP 8)

Binds and calls the closure

Description

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

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

Parameters

newThis

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

args

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

Return Values

Returns the return value of the closure.

Examples

Closure::call() example

php
<?php
class Value {
    protected $value;

    public function __construct($value) {
        $this->value = $value;
    }

    public function getValue() {
        return $this->value;
    }
}

$three = new Value(3);
$four = new Value(4);

$closure = function ($delta) { var_dump($this->getValue() + $delta); };
$closure->call($three, 4);
$closure->call($four, 4);
?>

The above example will output:

output
int(7)
int(8)

Source: language/predefined/closure/call.xml · from the official PHP manual (php/doc-en)