Closure::call

(PHP 7)
Binds and calls the closure
public mixed Closure::call ( object $newthis [, 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.

...

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

Returns:

Returns the return value of the closure.

Examples:
Closure::call() example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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:

int(7)
int(8)
doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.