最近在研究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. Python 项目实践三(Web应用程序)第五篇

    接着上节继续学习,在这一节,我们将建立一个用户注册和身份验证系统,让用户能够注册账户,进而登录和注销.我们将创建一个新的应用程序,其中包含与处理用户账户相关的所有功能.我们还将对模型Topic稍做修改 ...

  2. 前端之 HTML🎃

    HTML这知识点很多很杂,所以整理很乱.所以将就看.

  3. Android 获取唯一标识替代方法

    private static String getTheOnlyID() { String onlyOne; //获取IMEI TelephonyManager TelephonyMgr = (Tel ...

  4. 【Python3的函数初识】

    一.函数 1.什么是函数? 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率,可扩展性强. 2.函数的分类 在python中函数分两类:内 ...

  5. Linux知识--初始linux

    从今天开始陆续分享Linux的知识 因为服务器基本是Linux的 所以Linux不学明白  Shell命令不熟  会让你的办事效率大打折扣. 一.Linux文件系统 Linux文件系统是从Unix结构 ...

  6. css边框内圆角

    一.使用两个元素实现 html <div class="parent"> <div class="inset-radius">时代峰峻胜 ...

  7. Java 非线程安全的HashMap如何在多线程中使用

    Java 非线程安全的HashMap如何在多线程中使用 HashMap 是非线程安全的.在多线程条件下,容易导致死循环,具体表现为CPU使用率100%.因此多线程环境下保证 HashMap 的线程安全 ...

  8. HDFS Architecture

    http://hadoop.apache.org/docs/r2.9.0/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html Introduction Ha ...

  9. 使用腾讯云无服务器云函数(SCF)分析天气数据

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:李想 无服务器云函数(SCF)是腾讯云提供的Serverless执行环境,也是国内首款FaaS(Function as a Service ...

  10. commons -lang(2) RandomStringUtils RandomUtils

    上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html 1.RandomStringUtils 1.1模拟实现random ...