php7 闭包调用

早起的版本如 PHP5.6 ,绑定并调用闭包使用 bindTo,而PHP7 中 Closure :: call()方法具有更好的性能,废话不多说,

较早的 PHP 示例:

<?php
class Person {
    private $name = '乔峰';
}
$getName = function() {
    return $this->name;
};

// Bind a clousure
$value = $getName->bindTo(new Person, 'Person');

print($value());// 乔峰

PHP7 示例:

<?php
class Person {
    private $name = '乔峰';
}

$getName = function() {
    return $this->name;
};

print($getName->call(new Person));//乔峰 注意 这里可以直接是 new person,当然new person()也可以

 

posted @ 2018-09-16 14:48  周伯通之草堂  阅读(2434)  评论(1编辑  收藏  举报