PHP Closure類Bind與BindTo方法

Closure類爲閉包類,PHP中閉包都是Closure的實例:php

1     $func = function(){};
2     var_dump($func instanceof Closure);

輸出   bool(true)

Closure有兩個函數將閉包函數綁定到對象上去,laravel

靜態方法Bind閉包

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

動態方法BindTo函數

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

  

靜態閉包不能有綁定的對象($newthis 參數的值應該設爲 NULL )
測試

此時Closure不能夠使用$this。this

class Father 
{
    public $pu = "public variable";

    public static $spu = 'public static';
}

class Son extends Father
{

}

class Other 
{

}

$son = new Son();
$func = function(){
    echo self::$spu;
};

($func -> bindTo(null, 'Son'))();

靜態閉包中不能夠調用$this,不然會報錯。就像類的靜態方法不能夠調用$this同樣spa

    $son = new Son();
    $func = function(){
        echo $this -> $pu;
    };

    ($func -> bindTo(null, 'Son'))();
報錯:
Fatal error: Uncaught Error: Using $this when not in object context in D:\laravel\test.php:21
Stack trace:

類做用域:.net

當閉包綁定到對象上時,或者綁定到null成爲靜態對象,能夠經過返回的閉包對象來調用對象的方法,同時能夠設定第三個參數$newscope來設定對象中 code

屬性或方法對於閉包的訪問可見性。閉包的訪問可見性和$newscope類的成員函數是相同的。對象

    class Father 
    {
        protected $pu = "public variable";

        protected static $spu = 'public static';
    }

    class Son extends Father
    {

    }
        //Son中的方法能夠正常訪問Father類中的protected屬性

    class Other 
    {

    }
        //Other中的方法沒法訪問Father類中的protected屬性


測試:

    $son = new Son();

    $func = function(){
        echo $this -> pu;
    };

    (Closure::bind($func, $son, 'Son'))();
    輸出 public variable
    (Closure::bind($func, $son, 'Other'))();
    報錯 Fatal error: Uncaught Error: Cannot access protected property Son::$pu

  

匿名函數都是Closure的實例因此能夠調用 bindTo 方法。
bindTo方法
($func -> bindTo($son, 'Son'))();
($func -> bindTo($son, 'Other'))();     

$newscope默認爲‘Static'表示不改變,仍是以前的做用域。

class Grand
    {
        protected $Grandvar = 'this is grand';
    }

    class Father extends Grand
    {
        protected $Fathervar = "this is  Father";
    }

    class Mother extends Grand
    {
        protected $Mothervar = 'this is Mother';
    }

    class Son extends Father
    {
        protected $Sonvar = 'this is son';
    }

    $son = new Son();
    $mon = new Mother();

    $func = function(){
        echo $this -> Grandvar;
    };
    
        綁定訪問做用域爲'Son'的做用域,以後使用以前的默認做用域,
        因此能夠訪問Grandvar
    $newFunc = Closure::bind($func, $son, 'Son');
    $newFunc = Closure::bind($newFunc, $mon);
    $newFunc();

        未綁定訪問做用域,默認沒有權限
    $newFunc = Closure::bind($func, $mon);
    $newFunc();

http://php.net/manual/zh/class.closure.php

相關文章
相關標籤/搜索