* 在array_map()函数中使用闭包

<?php

$numbersPlusOne = array_map(function($number) {
return $number + 1;
}, [1,2,3]);
print_r($numbersPlusOne);

  

$ php numbersPlusOne.php

  

Array
(
[0] => 2
[1] => 3
[2] => 4
)

* 使用use关键字附加闭包的状态

<?php

function enclosePerson($name) {
// use 可以把多个参数传入闭包
return function($doCommand) use ($name) {
return sprintf('%s, %s'.PHP_EOL, $name, $doCommand);
};
} $clay = enclosePerson('Clay'); echo $clay('get me some sweet tea!');

  Clay, get me some sweet tea!

* 使用bindTo方法附加闭包的状态

<?php

class App {
protected $routes = [];
protected $responseStatus = '200 OK';
protected $responseContentType = 'text/html';
protected $responseBody = 'Hello world'; public function addRoute($routePath, $routeCallBack) {
$this->routes[$routePath] = $routeCallBack->bindTo($this, __CLASS__);
} public function dispatch($currrentPath) {
foreach ($this->routes as $routePath => $callback) {
if ($routePath === $currrentPath) {
$callback();
}
}
header('HTTP/1.1 ', $this->responseStatus);
header('Content-Type: ', $this->responseContentType);
header('Content-length: ', mb_strlen($this->responseBody));
echo $this->responseBody;
}
} $app = new App();
$app->addRoute('/users/josh', function() {
$this->responseContentType = 'application/json; charset=utf8';
$this->responseBody = '{"name": "Josh"}';
}); $app->dispatch('/users/josh');
echo PHP_EOL;
// {"name": "Josh"}

  

modern php closure 闭包的更多相关文章

  1. iOS - Swift Closure 闭包

    1.Closure 闭包在 Swift 中非常有用.通俗的解释就是一个 Int 类型里存储着一个整数,一个 String 类型包含着一串字符,同样,闭包是一个包含着函数的类型.有了闭包,你就可以处理很 ...

  2. access to modified closure 闭包的问题

    ; i < listBoxDevices.Items.Count; i++) { var tempDeviceId = listBoxDevices.Items[i].ToString(); i ...

  3. javascript closure 闭包 事件绑定

    先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...

  4. JS Closure 闭包

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

  5. Closure闭包示例

    var foo = function(){ var cnt = 0; return function(){ return cnt++; }; }; var closure = foo(); conso ...

  6. PHP Closure(闭包)类详解

    Closure 面向对象变成语言代码的复用主要采用继承来实现,而函数的复用,就是通过闭包来实现.这就是闭包的设计初衷. 注:PHP里面闭包函数是为了复用函数而设计的语言特性,如果在闭包函数里面访问指定 ...

  7. Php5.3的lambda函数以及closure(闭包)

    从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...

  8. ES3之closure ( 闭包 )

    词法作用域中使用的域,是变量在代码中声明的位置所决定的.嵌套的函数可以访问在其外部声明的变量. 闭包是函数和声明该函数的词法环境的组合. 1 创建单个闭包 JavaScript中的函数会形成闭包. 闭 ...

  9. javascript中的闭包closure详解

    目录 简介 函数中的函数 Closure闭包 使用闭包实现private方法 闭包的Scope Chain 闭包常见的问题 闭包性能的问题 总结 简介 闭包closure是javascript中一个非 ...

随机推荐

  1. S3C2440—12.按键中断

    文章目录 一. 总体 二. CPSR设置 三. 中断源设置 四. 中断控制器设置 五. C中断处理函数 六. 汇编IRQ异常处理程序 七. 源码 一. 总体 要驱动按键中断控制LED亮灭,程序要进行如 ...

  2. mongoTemplate 条件查询

    构建条件方法 @Override public Query getQuery(ReportParam param){ //check MeenoAssert.hasLength(param.getUu ...

  3. SpringBoot获取请求的参数

    说明 SpringBoot 为我们封装了许多,简便的获取请求参数的方法! 1.获取无注解获取请求参数 请求地址:http://192.168.0.115:8080/myproject/test/noA ...

  4. windows通过pfx文件生成key、crt文件

    nginx代理的时候,需要填写证书的crt跟rsa文件路径,通过iis导出的证书是pfx文件(不知道nginx能不能直接用pfx文件,没有查看过相关资料),所以要通过pfx文件生成crt.rsa文件. ...

  5. 关于Ubuntu18.04 linux系统下使用Tim QQ 微信

    先配上张图 步骤: 1.1 :需要安装环境deepin-wine 1.1:(你把他理解为jdk就好,没有jdk无法运行java程序,同理没有deepin-wine环境无法运行腾讯产品) 1.2 :去哪 ...

  6. vue去掉一些烦人的校验规则

    例如:括号前没有加空格报错,很难受 如何处理呢,故意犯错,然后打开页面出现错误信息,如下图复制错误 space-before-function-paren 找到项目中的.eslintrc.js 添加一 ...

  7. js判断checkbox是否选中 .checked不管用

    今天开发遇到一个小问题,记小本本记小本本 document.getElementById("id").checked //正确 //如果返回值为true代表选中 //如果返回值为f ...

  8. 2014 12 27 bestcoder 第一题

    水的不行不行的一道题 也是自己做的第一道题  纪念下 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h ...

  9. 刷题-力扣-剑指 Offer 42. 连续子数组的最大和

    剑指 Offer 42. 连续子数组的最大和 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de ...

  10. Spring Cloud总结

    restTemplate 消费者模块编写restTemplate配置类,即可在控制层调用提供者模块 // 配置类 @Configuration public class ApplicationCont ...