Closure::call

(PHP 7)

Closure::callBinds and calls the closure

Descrizione

public Closure::call ( object $newthis [, mixed $... ] ) : mixed

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

Elenco dei parametri

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.

Valori restituiti

Returns the return value of the closure.

Esempi

Example #1 Closure::call() example

<?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($three4);
$closure->call($four4);
?>

Il precedente esempio visualizzerĂ :

int(7)
int(8)
add a note add a note

User Contributed Notes 1 note

up
0
sergey dot nevmerzhitsky at gmail dot com
3 years ago
Prior PHP 7.0 you can use this code:

<?php
$cl
= function($add) { return $this->a + $add; };

$cl->bindTo($newthis);
return
call_user_func_array($cl, [10]);
?>

But this bind the closure permanently! Also read the article for Closure::bindTo() about binding closures from static context.
To Top