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

Example of using Closure::call in PHP7

autoload
Release: 2023-02-17 20:48:02
Original
2008 people have browsed it

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 $... ] )
Copy after login

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);
Copy after login

Example after php7:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template