比如你现在就可以这样使用: $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; };
比如在输出HTML中闭包很有用: function item_list(array $items, $formatter = null) { //create the default formatter if($formatter == null) { $formatter = function($row) { return '﹤p﹥' . $row . '﹤/p﹥'; }; } $html = '﹤h2﹥Listing:﹤/h2﹥'; foreach($items as $item) { $html .= $formatter($item); } return $html; }
(责任编辑:admin) |