php 7 的 closure::call() 有着更好的性能,作用:将一个闭包函数动态绑定到一个新的对象实例并调用执行该函数。
描述:
public mixed Closure::call ( object $newthis [, mixed $... ] )
暂时将闭包绑定到newthis,并用任何给定的参数调用。
php7之前的示例:
<?php
class A {
private $x = 1;
}
// PHP 7 之前版本定义闭包函数代码
$getXCB = function()
{
return $this->x;
};
// 闭包函数绑定到类 A 上
$getX = $getXCB->bindTo(new A, 'A');
echo $getX();
print(PHP_EOL);php7之后的示例:
立即学习“PHP免费学习笔记(深入)”;
<?php
class A {
private $x = 1;
}
$getX = function() {
return $this->x;
};
echo $getX->call(new A);
?>以上就是PHP7中Closure :: call的使用范例的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号