PHP鐨勫尶鍚嶅嚱鏁
鍐呭鎽樿
鍖垮悕鍑芥暟锛圓nonymous functions锛夛紝涔熷彨闂寘鍑芥暟锛坈losures锛夛紝鍏佽 涓存椂鍒涘缓涓涓病鏈夋寚瀹氬悕绉扮殑鍑芥暟銆傛渶缁忓父鐢ㄤ綔鍥炶皟鍑芥暟锛坈allback锛夌殑鍙傛暟銆 褰撶劧锛屼篃鏈夊叾浠栧簲鐢ㄧ殑鎯呭喌銆
鏂囩珷姝f枃
鍖垮悕鍑芥暟
鍖垮悕鍑芥暟锛圓nonymous functions锛夛紝涔熷彨闂寘鍑芥暟锛坈losures锛夛紝鍏佽 涓存椂鍒涘缓涓涓病鏈夋寚瀹氬悕绉扮殑鍑芥暟銆傛渶缁忓父鐢ㄤ綔鍥炶皟鍑芥暟锛坈allback锛夌殑鍙傛暟銆 褰撶劧锛屼篃鏈夊叾浠栧簲鐢ㄧ殑鎯呭喌銆
Example #1 Anonymous function 绀轰緥
<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// 杈撳嚭 helloWorld
?>
闂寘鍑芥暟涔熷彲浠ヤ綔涓哄彉閲忕殑鍊兼潵浣跨敤銆侾HP浼氳嚜鍔ㄦ妸琛ㄨ揪寮忚浆鎹㈡垚鍐呯疆绫籆losure鐨 瀵硅薄瀹炰緥銆傛妸涓涓猚losure瀵硅薄璧嬪肩粰涓涓彉閲忕殑鏂瑰紡涓庢櫘閫氬彉閲忚祴鍊肩殑璇硶鏄竴鏍风殑锛屾渶鍚庝篃瑕佸姞涓婂垎鍙枫
Example #2 鍖垮悕鍑芥暟鍙橀噺璧嬪肩ず渚
<?php
$greet = function($name)
{
printf("Hello %s ", $name);
};
$greet('World');
$greet('PHP');
?>
Closure瀵硅薄涔熶細浠庣埗浣滅敤鍩熶腑缁ф壙绫诲睘鎬с傝繖浜涘彉閲忛兘蹇呴』鍦ㄥ嚱鏁版垨绫荤殑澶撮儴澹版槑銆備粠鐖朵綔鐢ㄥ煙涓户鎵 鍙橀噺涓庝娇鐢ㄥ叏灞鍙橀噺鏄*涓嶅悓*鐨勩傚叏灞鍙橀噺瀛樺湪浜庝竴涓叏灞鐨勮寖鍥达紝鏃犺褰撳墠鍦ㄦ墽琛岀殑鏄摢涓嚱鏁般傝宑losure 鐨勭埗绫讳綔鐢ㄥ煙鍒欐槸澹版槑璇losure鐨勫嚱鏁帮紙涓嶄竴瀹氳鏄畠琚皟鐢ㄧ殑鍑芥暟锛夈傜ず渚嬪涓嬶細
Example #3 Closures鍜屼綔鐢ㄥ煙
<?php
// 涓涓熀鏈殑璐墿杞︼紝鍖呮嫭涓浜涘凡缁忔坊鍔犵殑鍟嗗搧鍜屾瘡绉嶅晢鍝佺殑鏁伴噺銆
// 鍏朵腑鏈変竴涓柟娉曠敤鏉ヨ绠楄喘鐗╄溅涓墍鏈夊晢鍝佺殑鎬讳环鏍笺傝鏂规硶浣跨敤浜嗕竴涓猚losure浣滀负鍥炶皟鍑芥暟銆
class Cart
{
const PRICE_BUTTER = 1.00;
const PRICE_MILK = 3.00;
const PRICE_EGGS = 6.95;
protected $products = array();
public function add($product, $quantity)
{
$this->products[$product] = $quantity;
}
public function getQuantity($product)
{
return isset($this->products[$product]) ? $this->products[$product] :
FALSE;
}
public function getTotal($tax)
{
$total = 0.00;
$callback =
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);;
}
}
$my_cart = new Cart;
// 寰璐墿杞﹂噷娣诲姞鏉$洰
$my_cart->add('butter', 1);
$my_cart->add('milk', 3);
$my_cart->add('eggs', 6);
// 鎵撳嚭鍑烘讳环鏍硷紝鍏朵腑鏈 5% 鐨勯攢鍞◣.
print $my_cart->getTotal(0.05) . " ";
// The result is 54.29
?>
鍖垮悕鍑芥暟鐩墠鏄氳繃func_num_args(), func_get_arg(), 鍜 func_get_args() 绛夊嚱鏁版潵鑾峰彇鍙傛暟淇℃伅銆
鍖垮悕鍑芥暟锛圓nonymous functions锛夛紝涔熷彨闂寘鍑芥暟锛坈losures锛夛紝鍏佽 涓存椂鍒涘缓涓涓病鏈夋寚瀹氬悕绉扮殑鍑芥暟銆傛渶缁忓父鐢ㄤ綔鍥炶皟鍑芥暟锛坈allback锛夌殑鍙傛暟銆 褰撶劧锛屼篃鏈夊叾浠栧簲鐢ㄧ殑鎯呭喌銆
Example #1 Anonymous function 绀轰緥
<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// 杈撳嚭 helloWorld
?>
闂寘鍑芥暟涔熷彲浠ヤ綔涓哄彉閲忕殑鍊兼潵浣跨敤銆侾HP浼氳嚜鍔ㄦ妸琛ㄨ揪寮忚浆鎹㈡垚鍐呯疆绫籆losure鐨 瀵硅薄瀹炰緥銆傛妸涓涓猚losure瀵硅薄璧嬪肩粰涓涓彉閲忕殑鏂瑰紡涓庢櫘閫氬彉閲忚祴鍊肩殑璇硶鏄竴鏍风殑锛屾渶鍚庝篃瑕佸姞涓婂垎鍙枫
Example #2 鍖垮悕鍑芥暟鍙橀噺璧嬪肩ず渚
<?php
$greet = function($name)
{
printf("Hello %s ", $name);
};
$greet('World');
$greet('PHP');
?>
Closure瀵硅薄涔熶細浠庣埗浣滅敤鍩熶腑缁ф壙绫诲睘鎬с傝繖浜涘彉閲忛兘蹇呴』鍦ㄥ嚱鏁版垨绫荤殑澶撮儴澹版槑銆備粠鐖朵綔鐢ㄥ煙涓户鎵 鍙橀噺涓庝娇鐢ㄥ叏灞鍙橀噺鏄*涓嶅悓*鐨勩傚叏灞鍙橀噺瀛樺湪浜庝竴涓叏灞鐨勮寖鍥达紝鏃犺褰撳墠鍦ㄦ墽琛岀殑鏄摢涓嚱鏁般傝宑losure 鐨勭埗绫讳綔鐢ㄥ煙鍒欐槸澹版槑璇losure鐨勫嚱鏁帮紙涓嶄竴瀹氳鏄畠琚皟鐢ㄧ殑鍑芥暟锛夈傜ず渚嬪涓嬶細
Example #3 Closures鍜屼綔鐢ㄥ煙
<?php
// 涓涓熀鏈殑璐墿杞︼紝鍖呮嫭涓浜涘凡缁忔坊鍔犵殑鍟嗗搧鍜屾瘡绉嶅晢鍝佺殑鏁伴噺銆
// 鍏朵腑鏈変竴涓柟娉曠敤鏉ヨ绠楄喘鐗╄溅涓墍鏈夊晢鍝佺殑鎬讳环鏍笺傝鏂规硶浣跨敤浜嗕竴涓猚losure浣滀负鍥炶皟鍑芥暟銆
class Cart
{
const PRICE_BUTTER = 1.00;
const PRICE_MILK = 3.00;
const PRICE_EGGS = 6.95;
protected $products = array();
public function add($product, $quantity)
{
$this->products[$product] = $quantity;
}
public function getQuantity($product)
{
return isset($this->products[$product]) ? $this->products[$product] :
FALSE;
}
public function getTotal($tax)
{
$total = 0.00;
$callback =
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);;
}
}
$my_cart = new Cart;
// 寰璐墿杞﹂噷娣诲姞鏉$洰
$my_cart->add('butter', 1);
$my_cart->add('milk', 3);
$my_cart->add('eggs', 6);
// 鎵撳嚭鍑烘讳环鏍硷紝鍏朵腑鏈 5% 鐨勯攢鍞◣.
print $my_cart->getTotal(0.05) . " ";
// The result is 54.29
?>
鍖垮悕鍑芥暟鐩墠鏄氳繃func_num_args(), func_get_arg(), 鍜 func_get_args() 绛夊嚱鏁版潵鑾峰彇鍙傛暟淇℃伅銆
浠ヤ笂灏辨槸PHP鐨勫尶鍚嶅嚱鏁扮殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒