最近在研究laravel5.5的源代码,发现了其中的一段代码觉得挺有意思!

文件:vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php

    public function then(Closure $destination)
{
$pipeline = array_reduce(
array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)
);
return $pipeline($this->passable);
}
    protected function carry()
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
if (is_callable($pipe)) {
// If the pipe is an instance of a Closure, we will just call it directly but
// otherwise we'll resolve the pipes out of the container and call it with
// the appropriate method and arguments, returning the results back out.
return $pipe($passable, $stack);
} elseif (! is_object($pipe)) {
list($name, $parameters) = $this->parsePipeString($pipe); // If the pipe is a string we will parse the string and resolve the class out
// of the dependency injection container. We can then build a callable and
// execute the pipe function giving in the parameters that are required.
$pipe = $this->getContainer()->make($name); $parameters = array_merge([$passable, $stack], $parameters);
} else {
// If the pipe is already an object we'll just make a callable and pass it to
// the pipe as-is. There is no need to do any extra parsing and formatting
// since the object we're given was already a fully instantiated object.
$parameters = [$passable, $stack];
}
return method_exists($pipe, $this->method)
? $pipe->{$this->method}(...$parameters)
: $pipe(...$parameters);
};
};
}

此段代码初看上去让人很迷惑,特作以下demo分解:

//闭包与array_reduce结合测试
$a = array( 1 , 2 , 3 , 4 , 5 );
$b = array_reduce ( $a , function($v , $w){
return function($p) use($v, $w){
var_dump($v);
};
} ); if(is_callable($b)){
$b('spring');
}

输出:

object(Closure)#5 (2) {
["static"]=>
array(2) {
["v"]=>
object(Closure)#4 (2) {
["static"]=>
array(2) {
["v"]=>
object(Closure)#3 (2) {
["static"]=>
array(2) {
["v"]=>
object(Closure)#2 (2) {
["static"]=>
array(2) {
["v"]=>
NULL
["w"]=>
int(1)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}
["w"]=>
int(2)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}
["w"]=>
int(3)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}
["w"]=>
int(4)
}
["parameter"]=>
array(1) {
["$p"]=>
string(10) "<required>"
}
}

PHP闭包Closure与array_reduce结合的一个范例的更多相关文章

  1. JavaScript闭包(Closure)

    JavaScript闭包(Closure) 本文收集了多本书里对JavaScript闭包(Closure)的解释,或许会对理解闭包有一定帮助. <你不知道的JavsScript> Java ...

  2. js中有趣的闭包(closure)

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  3. 聊一下JS中的作用域scope和闭包closure

    聊一下JS中的作用域scope和闭包closure scope和closure是javascript中两个非常关键的概念,前者JS用多了还比较好理解,closure就不一样了.我就被这个概念困扰了很久 ...

  4. python 函数对象(函数式编程 lambda、map、filter、reduce)、闭包(closure)

    1.函数对象 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函 ...

  5. 深入理解JavaScript闭包(closure)

    最近在网上查阅了不少javascript闭包(closure)相关的资料,写的大多是非常的学术和专业.对于初学者来说别说理解闭包了,就连文字叙述都很难看懂.撰写此文的目的就是用最通俗的文字揭开Java ...

  6. [转] Java内部类之闭包(closure)与回调(callback)

    闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域.通过这个定义,可以看出内部类是面向对象的闭包,因为它 不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥 ...

  7. JavaScript 进阶(四)解密闭包closure

    闭包(closure)是什么东西 我面试前端基本都会问一个问题"请描述一下闭包".相当多的应聘者的反应都是断断续续的词,“子函数”“父函数”“变量”,支支吾吾的说不清楚.我提示说如 ...

  8. javascript中的闭包(Closure)的学习

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面是我在网上通过学习阮一峰老师的笔记,感觉总结很不错,特记录于此. 一.变量的作用域 要理解 ...

  9. [转载]学习Javascript闭包(Closure)

    学习Javascript闭包(Closure)     源地址: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures ...

随机推荐

  1. Zabbix 监控数据库MSSqlServer

    zabbix  通过ODBC连接sql server,并通过odbc 获取数据从数据库: 配置如下: 在zabbix-server端 执行下面命令: # yum -y install freetds ...

  2. java 连接 postgresql

    最近公司用postgresql这个数据库,看网上说这个数据库还算好用,自己就用了一下,我就是用java连接了一下数据库. 其实每个数据库的连接方式大致相同,只是用到的驱动不同,用不同数据库只需要换不同 ...

  3. form表单样式

    <BODY> <div id="modify-data"> <form class="modify-data-form"> ...

  4. css-display

    1. none:隐藏对象.与visibility属性的hidden值不同,其不为被隐藏的对象保留其物理空间 2. inline:指定对象为内联元素. 3. block:指定对象为块元素. 4. inl ...

  5. 终端登入mysql

    mysql -u 用户名 -p 输入密码     1. 给root用户添加密码,密码为root     mysqladmin -u root -p password root 2. 通过终端连接ser ...

  6. php的二维数组排序

    //建立一个$arr的二维数组 $arr=array( array('name'=>'e','age'=>5), array('name'=>'b','age'=>2), ar ...

  7. 三十分钟学会AWK

    摘要: 本文大部分内容翻译自我开始学习AWK时看到的一篇英文文章 AWK Tutorial ,觉得对AWK入门非常有帮助,所以对其进行了粗略的翻译,并对其中部分内容进行了删减或者补充,希望能为对AWK ...

  8. centOS7-配置网络地址

    1.首先需求切换都root账户 # su #输入密码: 2.进入网络配置文件目录查看 ()进入该目录 # /etc/sysconfig/network-scripts ()查看目录下文件 # ll - ...

  9. WEB相关系列

    一.Nginx(web服务器) Nginx概述和安装(1) Nginx配置文件(2) Nginx日常维护操作(3) Nginx常用配置实例(4) Nginx常用功能(5) Nginx性能优化技巧(6) ...

  10. 记录Vue和Jquery混合开发中关于点击事件的一个bug

    最近比较急的接手了公司的微信服务号项目,采用的技术栈主要是jq和vue.在项目中之前碰见过jq写的$().on('click',function(){})点击事件不起作用,只能写在vue实例中的met ...