PHP 闭包(Closure)

2020-02-19 23:15:24

参考地址 PHP 闭包(Closure)


一、闭包基本用法

闭包(Closure)又叫做匿名函数,也就是没有定义名字的函数。比如下面的例子:


// 定义一个闭包,并把它赋给变量 $f

$f = function () {

    return 7;

}


// 使用闭包也很简单

$f(); //这样就调用了闭包,输出 7


// 当然更多的时候是把闭包作为参数(回调函数)传递给函数

function testClosure (Closure $callback) {

    return $callback();

}


// $f 作为参数传递给函数 testClosure,如果是普遍函数是没有办法作为testClosure的参数的

testClosure($f);


// 也可以直接将定义的闭包作为参数传递,而不用提前赋给变量

testClosure (function () {

    return 7;

});


// 闭包不止可以做函数的参数,也可以作为函数的返回值

function getClosure () {

    return function () { return 7; };

}


$c = getClosure(); // 函数返回的闭包就复制给 $c 了

$c(); // 调用闭包,返回 7

二、闭包类(Closure)

定义一个闭包函数,其实是产生了一个闭包类(Closure)的对象,Closure 类摘要如下:


Closure {   

    public static Closure bind (Closure $closure , object $newthis [, mixed $newscope = 'static' ])  

    public Closure bindTo (object $newthis [, mixed $newscope = 'static' ])  

方法说明:

Closure::bind: 复制一个闭包,绑定指定的 $this 对象和类作用域。

Closure::bindTo: 复制当前闭包对象,绑定指定的 $this 对象和类作用域。

下面将介绍 Closure::bind 和 Closure::bindTo

参数和返回值说明:

closure:表示需要绑定的闭包对象。

newthis:表示需要绑定到闭包对象的对象,或者 NULL 创建未绑定的闭包。

newscope:表示想要绑定给闭包的类作用域,可以传入类名或类的示例,默认值是'static', 表示不改变。

该方法成功时返回一个新的 Closure 对象,失败时返回 FALSE。


class Animal {  

    private static $cat = "cat";  

    private $dog = "dog";  

    public $pig = "pig";  

}  


/*  

 * 获取Animal类静态私有成员属性 

 */  

$cat = static function() {  

    return Animal::$cat;  

};  


/*  

 * 获取Animal实例私有成员属性 

 */  

$dog = function() {  

    return $this->dog;  

};  


/*  

 * 获取Animal实例公有成员属性 

 */  

$pig = function() {  

    return $this->pig;  

};  


$bindCat = Closure::bind($cat, null, new Animal());// 给闭包绑定了Animal实例的作用域,但未给闭包绑定$this对象  

$bindDog = Closure::bind($dog, new Animal(), 'Animal');// 给闭包绑定了Animal类的作用域,同时将Animal实例对象作为$this对象绑定给闭包  

$bindPig = Closure::bind($pig, new Animal());// 将Animal实例对象作为$this对象绑定给闭包,保留闭包原有作用域  

echo $bindCat(),'<br>';// 根据绑定规则,允许闭包通过作用域限定操作符获取Animal类静态私有成员属性  

echo $bindDog(),'<br>';// 根据绑定规则,允许闭包通过绑定的$this对象(Animal实例对象)获取Animal实例私有成员属性  

echo $bindPig(),'<br>';// 根据绑定规则,允许闭包通过绑定的$this对象获取Animal实例公有成员属性


// bindTo与bind类似,是面向对象的调用方式,这里只举一个,其他类比就可以

$bindCat = $cat->bindTo(null, 'Animal');

最后举一个 PHP 官网给出的例子


class A {

    function __construct($val) {

        $this->val = $val;

    }

    function getClosure() {

        //returns closure bound to this object and scope

        return function() { return $this->val; };

    }

}


$ob1 = new A(1);

$ob2 = new A(2);


$cl = $ob1->getClosure();

echo $cl(), "\n";

$cl = $cl->bindTo($ob2);

echo $cl(), "\n";

以上示例输出:


1

2



  • 2017-04-15 21:47:44

    Android开发笔记——圆角和边框们

    在做Android界面开发时,我们往往希望它尽可能优美,尽可能显得专业。于是你看了看其他应用,哇,好多边框和圆角啊。你是不是也想给自己的应用加上边框和圆角效果?呃……那怎么做呢?如果你是从web前端跑到Android来的,那么我想你一定想到了不下三种解决方案。如用图片替代,用CSS3定义,用JS画。在Android中,其实也有类似的用法,本文将简单介绍两种Android圆角和边框的实现。

  • 2017-04-15 21:49:06

    Android样式的开发:Style篇

    前面铺垫了那么多,终于要讲到本系列的终篇,整合所有资源,定义成统一的样式。 哪些该定义成统一的样式呢?举几个例子吧:

  • 2017-04-15 23:54:49

    ViewPager+Fragment取消预加载以及禁止滑动

    取消预加载 网上了解了很多取消预加载的方法,里面提到了使用一个viewpager的public方法setOffscreenPageLimit 经过查看源码以及验证发现该方法是管理Viewpager预加载的页数,最低也是默认为一页(例如ViewPager一共有4页,当前手机屏幕显示第一页

  • 2017-04-15 23:56:30

    onInterceptTouchEvent和onTouchEvent调用关系详解

    如果没有onInterceptTouchEvent,只考虑onTouchEvent的话,比较容易分析和理解。假如有三层布局结构,linearLayout1,linearLayout2,textView,从前到后是包含的关系。那么下面分情况说明。

  • 2017-04-16 19:36:32

    ViewPager预加载问题和onCreateView多次调用问题的解决

    1,在使用ViewPager嵌套Fragment的时候,由于VIewPager的几个Adapter的设置来说,都会有一定的预加载(默认是左右各一个Frament)。通过设置setOffscreenPageLimit(int number) 来设置预加载的熟练,在V4包中,默认的预加载是1,即使你设置为0,也是不起作用的,设置的只能是大于1才会有效果的。我们需要通过更改V4包中的默认属性才可以

  • 2017-04-16 21:02:55

    ImageView的android:adjustViewBounds属性

    取值为true时: Adjust the ImageView's bounds to preserve the aspect ration of its drawable. 调整ImageView的界限来保持图像纵横比不变。 这并不意味着ImageView的纵横比就一定和图像的纵横比相同