-
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";
- ?>
-
相关阅读:
HDU 5338(ZZX and Permutations-用线段树贪心)
编程之美-活动中心(三分)
form的method用get导致中文乱码
Tomcat: Could not clean server of obsolete files
Eclipse打开javadoc框
Java EE各种documentation
web-project的/WEB-INF/lib
在jsp里面不要瞎用<!-- -->注释
[流水账]搜索与web-container版本匹配的jar包
session的创建与销毁
-
原文地址:https://www.cnblogs.com/xiaoleiel/p/8324246.html
Copyright © 2020-2023
润新知