Closure::call

Closure::call

(PHP 7)

Closure::callBinds and calls the closure

Description

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.

Return Values

Returns the return value of the closure.

Examples

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($three, 4);
$closure->call($four, 4);
?>

The above example will output:

int(7)
int(8)

© 1997–2017 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://secure.php.net/manual/en/closure.call.php

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部