* 在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. h5与小程序互相跳转,传参和获取参数

    1.h5跳转到小程序 首先引入js文件 <script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js">< ...

  2. excel控件只为简单写入数据表--github找到ExcelUtil笔记

    github地址 https://github.com/SargerasWang/ExcelUtil 文档地址 https://sargeraswang.com/blog/2018/11/27/exc ...

  3. 关于Java for循环的注意点

    1 import java.util.ArrayList; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 ...

  4. ObjectInputStream和ObjectOutputStream

    package stream.object; import java.io.FileInputStream; import java.io.FileOutputStream; import java. ...

  5. redis连接池 go-redis

    为什么使用连接池? 首先Redis也是一种数据库,它基于C/S模式,因此如果需要使用必须建立连接,稍微熟悉网络的人应该都清楚地知道为什么需要建立连接,C/S模式本身就是一种远程通信的交互模式,因此Re ...

  6. C# ArrayPool 源码解读之 byte[] 池化

    一:背景 1. 讲故事最近在分析一个 dump 的过程中发现其在 gen2 和 LOH 上有不少size较大的free,仔细看了下,这些free生前大多都是模板引擎生成的html片段的byte[]数组 ...

  7. 眼见为实,看看MySQL中的隐藏列!

    在介绍mysql的多版本并发控制mvcc的过程中,我们提到过mysql中存在一些隐藏列,例如行标识.事务ID.回滚指针等,不知道大家是否和我一样好奇过,要怎样才能实际地看到这些隐藏列的值呢? 本文我们 ...

  8. CSS截取字段,让过长的字段结尾变成省略号(IE有效)

    text-overflow:ellipsis;overflow:hidden;<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transiti ...

  9. Nginx的高级使用

    1.概述 之前介绍过Nginx的简单使用,今天来聊聊Nginx的一些高级使用. 2.使用Nginx解决跨域问题 当公司存在多个域名时,两个不同的域名相互访问就会存在跨域问题. 或者在进行前端开发时,通 ...

  10. 【Elasticsearch】.NetCore中Elasticsearch组件NEST的使用

    .NetCore中Elasticsearch组件NEST的使用 1. 安装Docker # 安装Docker curl -fsSL https://get.docker.com | bash -s d ...