Home  >  Article  >  Backend Development  >  php的匿名函数是指什么

php的匿名函数是指什么

PHPz
PHPzOriginal
2016-06-06 20:51:431712browse

php的匿名函数是指什么

php的匿名函数是指什么?

php匿名函数和闭包的理解

PHP匿名函数和闭包使用的句法与普通函数相同,但匿名函和闭包数其实是伪装成函数的对象.

匿名函数:就是没有名称的函数.匿名函数可以赋值给变量,对象传递.不过匿名函数仍是函数,因此可以调用,还可以传入参数.匿名函数特别适合作为函数或方法的回调.

闭包:是指在创建时封装周围状态的函数.即使闭包所在的环境不存在了,闭包中封装的状态依然存在.

注意:理论上讲,闭包和匿名函数是不同的概念. 不过,PHP将其视作相同的概念.

 

匿名函数的语法:

 $func = function(){
     
 };//带结束符

示例demo:

 $func = function($param){
     echo($param);
 };
 $func('hello world');

更多相关技术文章,请访问PHP中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn