• php 闭包的理解


    参考: https://www.cnblogs.com/WuNaiHuaLuo/p/4928524.html,

           https://www.cnblogs.com/echojson/p/10957362.html

    闭包:单独理解为一种行为 ,将匿名函数在普通函数中当做参数传入,也可以被返回,调用的行为叫做闭包

    //例一
    //在函数里定义一个匿名函数,并且调用它
    function printStr() {
        $func = function( $str ) {
            echo $str;
        };
        $func( 'some string' );
    }printStr();
      
    //例二
    //在函数中把匿名函数返回,并且调用它
    function getPrintStrFunc() {
        $func = function( $str ) {
            echo $str;
        };
        return $func;
    }
    $printStrFunc = getPrintStrFunc();
    $printStrFunc( 'some string' );
      
     
    //例三
    //把匿名函数当做参数传递,并且调用它
    function callFunc( $func ) {
        $func( 'some string' );
    }
    $printStrFunc = function( $str ) {
        echo $str;
    };
    callFunc( $printStrFunc );
    //也可以直接将匿名函数进行传递。如果你了解js,这种写法可能会很熟悉
    callFunc( function( $str ) {
        echo $str;
    } );

    匿名函数:

    1 。概念手册上的解释匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。

    2 。 匿名函数目前是通过 Closure 类来实现的。闭包函数也可以作为变量的值来使用。PHP 会自动把此种表达式转换成内置类 Closure 的对象实例。即一个匿名函函数就是一个Closure实例, 把一个 closure 对象(匿名函数)赋值给一个变量的方式与普通变量赋值的语法是一样的,最后也要加上分号;

    //例二
    //在函数中把匿名函数返回,并且调用它
    function getPrintStrFunc() {
        $func = function( $str ) {
            echo $str;
        };
        return $func;
    }
    $printStrFunc = getPrintStrFunc();
    $printStrFunc( 'some string' );
    匿名函数的调用加上括号就行了

    Closure :bind方法的使用

    bind  当匿名函数中有 $this static 等引用  需要绑定指定的对象时 使用bind方法

    参数 

    closure

    需要绑定的匿名函数。

    newthis

    需要绑定到匿名函数的对象,或者 NULL 创建未绑定的闭包。

    newscope

    想要绑定给闭包的类作用域,或者 'static' 表示不改变。如果传入一个对象,则使用这个对象的类型名。 类作用域用来决定在闭包中 $this 对象的 私有、保护方法 的可见性。 The class scope to which associate the closure is to be associated, or 'static' to keep the current one. If an object is given, the type of the object will be used instead. This determines the visibility of protected and private methods of the bound object.

    返回值

    返回一个新的 Closure 对象 或者在失败时返回 FALSE

    Example #1 Closure::bind() 实例
    
    
    <?php
    class A {
        private static $sfoo = 1;
        private $ifoo = 2;
    }
    $cl1 = static function() {
        return A::$sfoo;
    };
    $cl2 = function() {
        return $this->ifoo;
    };
    
    $bcl1 = Closure::bind($cl1, null, 'A');
    $bcl2 = Closure::bind($cl2, new A(), 'A');
    echo $bcl1(), "
    ";
    echo $bcl2(), "
    ";
    ?>  
    
    
    以上例程的输出类似于:
    
    
    1
    2

    bindTo方法

    public Closure Closure::bindTo( object $newthis[, mixed $newscope  = 'static'] )
    
    创建并返回一个 匿名函数,它与当前对象的函数体相同、绑定了同样变量,但可以绑定不同的对象,也可以绑定新的类作用域。 
    
    "绑定的对象"决定了函数体中的 $this 的取值,"类作用域"代表一个类型、决定在这个匿名函数中能够调用哪些 私有 和 保护 的方法。也就是说,此时 $this 可以调用的方法,与 newscope 类的成员函数是相同的。 
    
    静态闭包不能有绑定的对象( newthis 参数的值应该设为 NULL)不过仍然可以用 bubdTo 方法来改变它们的类作用域。 
    
    This function will ensure that for a non-static closure, having a boundinstance will imply being scoped and vice-versa. To this end,non-static closures that are given a scope but a NULL instance are madestatic and non-static non-scoped closures that are given a non-nullinstance are scoped to an unspecified class. 
    
    
    Note: 
    
    如果你只是想要复制一个匿名函数,可以用 cloning 代替。 
    
    
    
    参数
    newthis
    绑定给匿名函数的一个对象,或者 NULL 来取消绑定。 
    newscope
    关联到匿名函数的类作用域,或者 'static' 保持当前状态。如果是一个对象,则使用这个对象的类型为心得类作用域。这会决定绑定的对象的 保护、私有成员 方法的可见性。 
    
    
    返回值
    
    返回新创建的 Closure 对象或者在失败时返回 FALSE 
    
    
    范例
    
    
    Example #1 Closure::bindTo() 实例
    
    
    <?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(), "
    ";
    $cl = $cl->bindTo($ob2);
    echo $cl(), "
    ";
    ?>  
    
    
    以上例程的输出类似于:
    
    
    1
    2
  • 相关阅读:
    构建之法阅读笔记一
    第一冲刺阶段 工作总结 02
    第一冲刺阶段 工作总结 01
    学习进度条 第七周
    团队计划会议 01
    团队博客 一 需求分析
    学习进度条 第六周
    数组练习3 求最大连通子数组的和
    03构建之法阅读笔记之三
    团队项目个人每日总结(4.19)
  • 原文地址:https://www.cnblogs.com/mofei12138/p/11892150.html
Copyright © 2020-2023  润新知