PHP 5.3 closure syntax is an anonymous function, which can bring many different experiences to developers. This article introduces several ways to use it. It is slightly different from JavaScript closures. However, compared to , for example, you can now use PHP 5.3 closure syntax like this
- $ closure = function($param) { echo $param; };
-
- //This one takes value of someVar and "stores" it in the closure's scope even if
- / /we later change the value of someVar outside it. We assume that $somerVar is defined before this
-
$closure2 = function($param) use ($someVar) { echo $param . ' ' . $someVar; };
For example, PHP 5.3 closure syntax is useful for closures in output HTML:
- functionitem_list(array $items, $formatter = null) {
-
-
if($formatter == null) {
-
function($row) { >return
-
'
'
. $row . > }; } html = 'Listing:
'
;
-
- foreach
- ($items as
$item-
) { $html .= $formatter($item); } return
-
$html; The above is the use of two different PHP 5.3 closure syntax introduced in this article. I hope it will be helpful to everyone.
http://www.bkjia.com/PHPjc/446435.html
www.bkjia.com- true
http: //www.bkjia.com/PHPjc/446435.html- TechArticle
PHP 5.3 closure syntax is an anonymous function, which can bring many different experiences to developers. This article introduces several ways to use it. It is slightly different from JavaScript's closure...-