-
php 5.3新增的闭包语法介绍function() use() {}
- <?php
- function callback($callback) {
- $callback();
- }
- callback(function() {
- print "This is a anonymous function.<br />/n";
- });
- $msg = "Hello, everyone";
- $callback = function () use ($msg) {
- print "This is a closure use string value, msg is: $msg. <br />/n";
- };
- $msg = "Hello, everybody";
- callback($callback);
- $msg = "Hello, everyone";
- $callback = function () use (&$msg) {
- print "This is a closure use string value lazy bind, msg is: $msg. <br />/n";
- };
- $msg = "Hello, everybody";
- callback($callback);
- $obj = (object) "Hello, everyone";
- $callback = function () use ($obj) {
- print "This is a closure use object, msg is: {$obj->scalar}. <br />/n";
- };
- $obj = (object) "Hello, everybody";
- callback($callback);
- $obj = (object) "Hello, everyone";
- $callback = function () use ($obj) {
- print "This is a closure use object, msg is: {$obj->scalar}. <br />/n";
- };
- $obj->scalar = "Hello, everybody";
- callback($callback);
- $obj = (object) "Hello, everyone";
- $callback = function () use (&$obj) {
- print "This is a closure use object lazy bind, msg is: {$obj->scalar}. <br />/n";
- };
- $obj = (object) "Hello, everybody";
- callback($callback);
- function counter() {
- $counter = 1;
- return function() use(&$counter) {return $counter ++;};
- }
- $counter1 = counter();
- $counter2 = counter();
- echo "counter1: " . $counter1() . "<br />/n";
- echo "counter1: " . $counter1() . "<br />/n";
- echo "counter1: " . $counter1() . "<br />/n";
- echo "counter1: " . $counter1() . "<br />/n";
- echo "counter2: " . $counter2() . "<br />/n";
- echo "counter2: " . $counter2() . "<br />/n";
- echo "counter2: " . $counter2() . "<br />/n";
- echo "counter2: " . $counter2() . "<br />/n";
- ?>
-
相关阅读:
简单 android popupwindow 实现
Android json 数据解析
java 重载父类报错 Remove '@override' annotation解决办法
Android数据的四种存储方式
Java将Unix时间戳转换成指定格式日期
Android中MenuInflater实例
技术转载:Jni学习四:如何编写jni方法
android NDk环境编译总结
Android JNI学习之javah命令的正确使用(找了好半天才找到的,汉,网上好多说法都没用)
android项目中如何加载已有so库 <转>
-
原文地址:https://www.cnblogs.com/raobenjun/p/7424656.html
Copyright © 2020-2023
润新知