最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP基于Closure類創(chuàng)建匿名函數(shù)的方法詳解

 更新時(shí)間:2017年08月17日 09:57:24   作者:奔跑的Man  
這篇文章主要介紹了PHP基于Closure類創(chuàng)建匿名函數(shù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Closure 類的功能、常用函數(shù)使用技巧及相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了PHP基于Closure類創(chuàng)建匿名函數(shù)的方法。分享給大家供大家參考,具體如下:

Closure 類

用于代表匿名函數(shù)的類。

匿名函數(shù)(在 PHP 5.3 中被引入)會(huì)產(chǎn)生這個(gè)類型的對(duì)象。在過去,這個(gè)類被認(rèn)為是一個(gè)實(shí)現(xiàn)細(xì)節(jié),但現(xiàn)在可以依賴它做一些事情。自 PHP 5.4 起,這個(gè)類帶有一些方法,允許在匿名函數(shù)創(chuàng)建后對(duì)其進(jìn)行更多的控制。

這個(gè)類不能實(shí)例化,里面主要有兩個(gè)方法,都用來復(fù)制閉包,一個(gè)靜態(tài)一個(gè)動(dòng)態(tài),下面分別詳細(xì)講解下這兩個(gè)不好理解的方法。

Closure::bind

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

參數(shù)說明:

closure
需要綁定的匿名函數(shù)。

newthis
需要綁定到匿名函數(shù)的對(duì)象,或者 NULL 創(chuàng)建未綁定的閉包。

newscope
想要綁定給閉包的類作用域,或者 'static' 表示不改變。如果傳入一個(gè)對(duì)象,則使用這個(gè)對(duì)象的類型名。 類作用域用來決定在閉包中 $this 對(duì)象的 私有、保護(hù)方法 的可見性。

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.

上面是該方法的定義,第一個(gè)參數(shù)很好理解,就是一個(gè)閉包函數(shù);第二個(gè)參數(shù)就不太好理解,如果要復(fù)制的閉包中包含$this,這個(gè)對(duì)象就表示這個(gè)$this,閉包函數(shù)里面對(duì)這個(gè)對(duì)象的修改在調(diào)用結(jié)束之后也會(huì)保持一致,比如修改了一個(gè)屬性;第三個(gè)參數(shù)就不太好理解了,看官方的說明也是云里霧里的,默認(rèn)參數(shù)情況下,調(diào)用$this->訪問object $newthis中的屬性函數(shù)的時(shí)候,會(huì)有限制,只能訪問public屬性的函數(shù),如果想訪問protected/private屬性,就要設(shè)置為對(duì)應(yīng)的類名/類實(shí)例,就要像在類里面一樣,要訪問那個(gè)類的保護(hù)/私有屬性函數(shù)。

例子

<?php
class T {
  private function show()
  {
    echo "我是T里面的私有函數(shù):show\n";
  }
  protected function who()
  {
    echo "我是T里面的保護(hù)函數(shù):who\n";
  }
  public function name()
  {
    echo "我是T里面的公共函數(shù):name\n";
  }
}
$test = new T();
$func = Closure::bind(function(){
  $this->who();
  $this->name();
  $this->show();
}, $test);
$func();

上面的代碼會(huì)報(bào)錯(cuò)Fatal error: Uncaught Error: Call to protected method T::who() from context 'Closure'。

加上bind第三個(gè)參數(shù)為t::class或者new T(),會(huì)正常輸出每一個(gè)結(jié)果。

我是T里面的保護(hù)函數(shù):who
我是T里面的公共函數(shù):name
我是T里面的私有函數(shù):show

當(dāng)然了,閉包也可以傳遞參數(shù)

$test = new StdClass();
var_dump($test);
$func = Closure::bind(function($obj){
  $obj->name = "燕睿濤";
}, null);
$func($test);
var_dump($test);

上面的程序跟匿名函數(shù)一樣,啥對(duì)象也沒有依賴,上面的程序會(huì)輸出:

object(stdClass)#1 (0) {
}
object(stdClass)#1 (1) {
 ["name"]=>
 string(9) "燕睿濤"
}

另外還有個(gè)特別要說明的例子

<?php
class T {
  private function show()
  {
    echo "我是T里面的私有函數(shù):show\n";
  }
  protected function who()
  {
    echo "我是T里面的保護(hù)函數(shù):who\n";
  }
  public function name()
  {
    echo "我是T里面的公共函數(shù):name\n";
  }
}
$func = Closure::bind(function ($obj) {
  $obj->show();
}, null);
$test = new T();
$func($test);

上面的情況會(huì)輸出什么呢,沒錯(cuò),會(huì)報(bào)錯(cuò),提示訪問不了私有屬性show,這個(gè)時(shí)候,加上第三個(gè)參數(shù)就可以了,看了第三個(gè)參數(shù)不光影響$this的作用域,也可以影響參數(shù)的作用域。

Closure::bindTo

bindTo和bind功能類似,這里只是另外一種形式,都是復(fù)制當(dāng)前閉包對(duì)象,綁定指定的$this對(duì)象和類作用域。,參數(shù)比bind少了第一個(gè),后面兩個(gè)一樣,當(dāng)然還有一個(gè)區(qū)別就是bindTo不是靜態(tài)方法,是閉包才會(huì)存在的一個(gè)屬性方法。

例子

<?php
class T {
  private function show()
  {
    echo "我是T里面的私有函數(shù):show\n";
  }
  protected function who()
  {
    echo "我是T里面的保護(hù)函數(shù):who\n";
  }
  public function name()
  {
    echo "我是T里面的公共函數(shù):name\n";
  }
}
$func = function () {
  $this->show();
  $this->who();
  $this->name();
};
$funcNew = $func->bindTo(new T(), T::class);
$funcNew();

上面函數(shù)的輸出和bind的類似

我是T里面的私有函數(shù):show
我是T里面的保護(hù)函數(shù):who
我是T里面的公共函數(shù):name

一個(gè)trick

這個(gè)函數(shù)是在看composer生成的自動(dòng)加載源碼的時(shí)候碰到的,在composer中用的比較特別,下面是截取部分composer中的代碼

// 文件autoload_real.php
call_user_func(\Composer\Autoload\ComposerStaticInit898ad46cb49e20577400c63254121bac::getInitializer($loader));
// 文件autoload_static.php
public static function getInitializer(ClassLoader $loader)
{
  return \Closure::bind(function () use ($loader) {
    $loader->prefixLengthsPsr4 = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$prefixLengthsPsr4;
    $loader->prefixDirsPsr4 = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$prefixDirsPsr4;
    $loader->prefixesPsr0 = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$prefixesPsr0;
    $loader->classMap = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$classMap;
  }, null, ClassLoader::class);
}

上面的代碼比較奇特,在call_user_func中,第一感覺是傳錯(cuò)參數(shù)了,其實(shí)不然,這里調(diào)用了一個(gè)函數(shù),這個(gè)函數(shù)會(huì)返回一個(gè)Closure對(duì)象,也就是一個(gè)匿名函數(shù),最終傳入的參數(shù)還是一個(gè)callable類型。再看看這個(gè)返回的閉包,里面使用了use,這是連接閉包和外部變量的橋梁。

至于這里為什么普通傳參數(shù)就可以,是因?yàn)閜hp5里面,對(duì)象形參和實(shí)參數(shù)指向相同的對(duì)象,函數(shù)里面對(duì)對(duì)象的修改會(huì)反映到對(duì)象外面。

所以,上面這么做是沒問題的,還有另外一種形式也可以

call_user_func(\Composer\Autoload\ComposerStaticInit898ad46cb49e20577400c63254121bac::getInitializer(), $loader);
public static function getInitializer()
{
  return \Closure::bind(function ($loader) {
    $loader->prefixLengthsPsr4 = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$prefixLengthsPsr4;
    $loader->prefixDirsPsr4 = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$prefixDirsPsr4;
    $loader->prefixesPsr0 = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$prefixesPsr0;
    $loader->classMap = ComposerStaticInit25885cdf386fdaafc0bce14bb5a7d06e::$classMap;
  }, null, ClassLoader::class);
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php常用函數(shù)與技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《php程序設(shè)計(jì)算法總結(jié)

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

舒兰市| 德钦县| 个旧市| 平湖市| 汾西县| 安化县| 灌南县| 遂宁市| 玛多县| 永靖县| 民勤县| 和政县| 靖远县| 察哈| 和林格尔县| 儋州市| 革吉县| 鸡西市| 宝应县| 穆棱市| 连江县| 东宁县| 台东市| 拉萨市| 茂名市| 茌平县| 尼勒克县| 运城市| 仁布县| 亳州市| 泉州市| 蓝田县| 德兴市| 汝州市| 柘城县| 清水河县| 砚山县| 大城县| 景德镇市| 桃园市| 闵行区|