PHP - 闭包Closure和lambda function
现在的语言没有闭包简直都不好意思说出来。
想要了解闭包是什么,那么就必须知道匿名函数。其实看起来他们其实差不多一个意思。
见php RFC一句话:
End of 2007 a patch was proposed that would add lambda functions( but without closures) to PHP. During the discussion on the mailing list, several people suggested that without support for closures, lambda functions are not useful enough to add them to PHP. This proposal describes a viable method of add lambda functions with closure support to PHP.
大概意思是,很早以前PHP只能使用匿名函数。但是呢没有闭包的匿名函数等于没什么用处,所以后来就加上了闭包。
1. 匿名函数有什么作用呢?
lambda functions allow the definition of throwaway functions that are not used anywhere
也就是说,匿名函数的作用就是能够简便快速的写那些只用一次的函数。
如果没有匿名函数,可能会出现如下情况:

如果有的话,我想大部分人都会直接写:
return preg_replace_callback('//', funciton($match) {xxx},$text)
这个就是匿名函数的作用。不过个人看法: 有了匿名函数之后,变量相当于有了CallBack这个类型。其实对于以前的php是一个比较大的改动,我们可以直接引用函数。我是这么理解的。
2. Closures闭包
Closures provide a very useful tool in order to make lambda functions even more useful.
闭包使得匿名函数更有作用,总的来说就是给匿名函数提供了一个访问父级上下文的功能。并且通用闭包的特性就是会保存上下文中引用过的值。
如下面的例子:引用了上下文中的$search和$replacement

可能这样看起来并不特别,下面会详细介绍。
值得注意的是:闭包的生命周期
Support for references are necessary in order to achieve true closures( like javascript, where a variable originating in parent scope can be modified by closures) while copying per default fits best with the current semantics of PHP and does not cause headaches in loop( for example, when importing a loop index into a closure).
在OOP中的Closure 默认可以访问 Class里面的 $this 指针,而不用像上面那样使用use来直接引入。
PHP - 闭包Closure和lambda function的更多相关文章
- 闭包(Closure)和匿名函数(Anonymous function)/lambda表达式的区别
闭包(Closure)和匿名函数(Anonymous function)/lambda表达式的区别 函数最常见的形式是具名函数(named function): function foo(){ con ...
- python 函数对象(函数式编程 lambda、map、filter、reduce)、闭包(closure)
1.函数对象 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(7)|函数Functions与闭包Closure]
[易学易懂系列|rustlang语言|零基础|快速入门|(7)函数Functions与闭包Closure] 有意思的基础知识 函数Functions与闭包Closure 我们今天再来看看函数. 在Ru ...
- JavaScript闭包(Closure)
JavaScript闭包(Closure) 本文收集了多本书里对JavaScript闭包(Closure)的解释,或许会对理解闭包有一定帮助. <你不知道的JavsScript> Java ...
- 聊一下JS中的作用域scope和闭包closure
聊一下JS中的作用域scope和闭包closure scope和closure是javascript中两个非常关键的概念,前者JS用多了还比较好理解,closure就不一样了.我就被这个概念困扰了很久 ...
- 深入理解JavaScript闭包(closure)
最近在网上查阅了不少javascript闭包(closure)相关的资料,写的大多是非常的学术和专业.对于初学者来说别说理解闭包了,就连文字叙述都很难看懂.撰写此文的目的就是用最通俗的文字揭开Java ...
- [转] Java内部类之闭包(closure)与回调(callback)
闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域.通过这个定义,可以看出内部类是面向对象的闭包,因为它 不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥 ...
- JavaScript 进阶(四)解密闭包closure
闭包(closure)是什么东西 我面试前端基本都会问一个问题"请描述一下闭包".相当多的应聘者的反应都是断断续续的词,“子函数”“父函数”“变量”,支支吾吾的说不清楚.我提示说如 ...
- [转载]学习Javascript闭包(Closure)
学习Javascript闭包(Closure) 源地址: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures ...
随机推荐
- jquery 给新增的addClass 使用css样式
假如有一个情况,当导航url找不到相同的地址,就会出现找不到地址,高亮当前导航不出现 解决 办法 记得使用文档载入完成后执行的函数. 因为 .current 是 addClass 新增的class $ ...
- 《DSP using MATLAB》示例Example 8.1
终于看到第8章了,加油,继续努力! N为奇数,有极点位于实数轴上.事实上,所有极点位于Ωc=0.5为半径的圆上,而不是单位圆.
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递 【转】
原文:http://www.cnblogs.com/lori/p/4045633.html 下面定义一个复杂类型对象 public class User_Info { public int Id { ...
- Linux下gdb线程的调试
多线程的调试命令 1.info threads: 这条命令显示的是当前可调试的所有线程,GDB会给每一个线程都分配一个ID.前面有*的线程是当前正在调试的线程. 2.thread ID: 切换到当前调 ...
- linux tcpdump命令抓包
tcpdump host 210.27.48.1 and \ (210.27.48.2 or 210.27.48.3 \) 截获主机210.27.48.1 和主机210.27.48.2 或210.27 ...
- redis+php实现微博功能(二)
数据结构: set post:postid:3:time timestampset post:postid:3:userid 5 set post:postid:3:content 测试发布哈哈哈哈 ...
- flask之bootstrap4
pip install bootstrap-flask from flask_bootstrap import Bootstrap from flask import Flask bootstra ...
- C# 操作iis6、iis7 301
iis6版本方法... iis7以及以上版本方法 using (ServerManager serverManager = new ServerManager()) { ...
- java代码啊==indexOf()方法返回字符第一次出现的位置
package com.s.x; public class Wang { public static void main(String[] args) { if ("woaini" ...
- Springboot监控之一:SpringBoot四大神器之Actuator之2--springboot健康检查
Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring Boot 内置了一些 HealthIndicator. ...