新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
PHP5.3閉包語法初探
推薦:PHP+MySQL視頻教程

PHP 5.3 將加入閉包語法,也就是匿名函數(shù),允許開發(fā)者申明行內(nèi)函數(shù)和在變量中保存。雖然這個語法和JavaScript的閉包相比有點怪異,但是對于PHP語言來說,這是一個良好的補充。
比如你現(xiàn)在就可以這樣使用:
$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;
}
|
網(wǎng)站題目:PHP5.3閉包語法初探
文章來源:http://fisionsoft.com.cn/article/coocede.html


咨詢
建站咨詢
