modern php closure 闭包
* 在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 闭包的更多相关文章
- iOS - Swift Closure 闭包
1.Closure 闭包在 Swift 中非常有用.通俗的解释就是一个 Int 类型里存储着一个整数,一个 String 类型包含着一串字符,同样,闭包是一个包含着函数的类型.有了闭包,你就可以处理很 ...
- access to modified closure 闭包的问题
; i < listBoxDevices.Items.Count; i++) { var tempDeviceId = listBoxDevices.Items[i].ToString(); i ...
- javascript closure 闭包 事件绑定
先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...
- JS Closure 闭包
/*一.变量的作用域要理解闭包,首先必须理解Javascript特殊的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量. ...
- Closure闭包示例
var foo = function(){ var cnt = 0; return function(){ return cnt++; }; }; var closure = foo(); conso ...
- PHP Closure(闭包)类详解
Closure 面向对象变成语言代码的复用主要采用继承来实现,而函数的复用,就是通过闭包来实现.这就是闭包的设计初衷. 注:PHP里面闭包函数是为了复用函数而设计的语言特性,如果在闭包函数里面访问指定 ...
- Php5.3的lambda函数以及closure(闭包)
从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...
- ES3之closure ( 闭包 )
词法作用域中使用的域,是变量在代码中声明的位置所决定的.嵌套的函数可以访问在其外部声明的变量. 闭包是函数和声明该函数的词法环境的组合. 1 创建单个闭包 JavaScript中的函数会形成闭包. 闭 ...
- javascript中的闭包closure详解
目录 简介 函数中的函数 Closure闭包 使用闭包实现private方法 闭包的Scope Chain 闭包常见的问题 闭包性能的问题 总结 简介 闭包closure是javascript中一个非 ...
随机推荐
- JVM学习笔记之class文件结构【七】
一.概念 1.1 无符号数: 以 u1.u2.u3.u4.u8 代表 1 个字节,2 个字节.4 个字节.8 个字节的无符号数.无符号数可以描述数字,索引引用.数量值和按照 UTF-8 编码构成的字符 ...
- Java中使用split方法根据英文问号?切割字符串时报错
因为正则表达式的原因,我们无法在java中直接使用String.split("?"),需要先转义其正确写法为: public static void splitStr() { St ...
- 题解 P3317 [SDOI2014]重建
题解 前置芝士:深度理解的矩阵树定理 矩阵树定理能求生成树个数的原因是,它本质上求的是: \[\sum_T \prod_{e\in T} w_e \] 其中 \(w_e\) 是边权,那么我们会发现其实 ...
- nginx 部署vue
server { listen 80; server_name localhost; root /www/meiduo_admin/dist; access_log /var/log/nginx/ad ...
- ORM 之 EF的使用(一)
早期对数据库进行操作 通过Ado.Net 操作数据库 需要操作sqlCommand/sqlConnection/adapter/datareader 如图 后来 基于面向对象的思想 出现了中间件ORM ...
- Java环境变量(Env)和系统属性(Property)详解
环境变量Env 使用System.getenv()获取系统的所有环境变量的Map,注意它是一个UnmodifiableCollection,是一个只读视图 环境变量并不提供set方法,即没有Syste ...
- offsetof宏---个人笔记
标准库里面提供的offsetof(t,m)宏,用来计算两个变量在内存中的地址偏移量 #include <stdio.h>//原型: #define offsetof(TYPE, MEMBE ...
- centos 搭建jenkins+git+maven
git+maven+jenkins持续集成搭建 发布人:[李源] 2017-12-08 04:33:37 一.搭建说明 系统:centos 6.5 jdk:1.8.0_144 jenkins ...
- 快速排序(C++)
快速排序 快速排序是面试中经常问到的排序算法 基本思想:通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小, 则可分别对这两部分记录继续进行排序,以达到整个序 ...
- python variable scope 变量作用域
python 中变量的作用域经常让我感到很迷 In Python, on the other hand, variables declared in if-statements, for-loop b ...