-
Notifications
You must be signed in to change notification settings - Fork 0
/
Closures.php
42 lines (34 loc) · 854 Bytes
/
Closures.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
$message = 'hello';
// 没有 "use"
$example = function () {
var_dump($message);
};
echo $example();
// 继承 $message
$example = function () use ($message) {
var_dump($message);
};
echo $example();
// Inherited variable's value is from when the function
// is defined, not when called
$message = 'world';
echo $example();
// Reset message
$message = 'hello';
// Inherit by-reference
$example = function () use (&$message) {
var_dump($message);
};
echo $example();
// The changed value in the parent scope
// is reflected inside the function call
$message = 'world';
echo $example();
// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
var_dump($arg . ' ' . $message);
};
$example("hello");
// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s