Home >Backend Development >PHP7 >Example of using Closure::call in PHP7

Example of using Closure::call in PHP7

autoload
autoloadOriginal
2021-03-17 11:12:532038browse

PHP 7's Closure::call() has better performance. Its function is to dynamically bind a closure function to a new one. Object instanceAnd call and execute the function.

Description:

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

Temporarily binds the closure to

newthis and calls it with any given arguments.

Example before php7:

<?php
class A {
    private $x = 1;
  }
// PHP 7 之前版本定义闭包函数代码
    $getXCB = function()
    {
    return $this->x;
    };
// 闭包函数绑定到类 A 上
$getX = $getXCB->bindTo(new A, &#39;A&#39;); 
echo $getX();
print(PHP_EOL);

Example after php7:

<?php
class A {
    private $x = 1;
  }
   $getX = function() {
        return $this->x;
  };
  echo $getX->call(new A);
 ?>

Recommended:

php video tutorial php7 tutorial

The above is the detailed content of Example of using Closure::call in PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn