PHP閉包函數(shù)詳解
匿名函數(shù)也叫閉包函數(shù)(closures允許創(chuàng)建一個(gè)沒有指定沒成的函數(shù),最經(jīng)常用作回調(diào)函數(shù)參數(shù)的值。
閉包函數(shù)沒有函數(shù)名稱,直接在function()傳入變量即可 使用時(shí)將定義的變量當(dāng)作函數(shù)來(lái)處理
$cl = function($name){ return sprintf('hello %s',name); } echo $cli('fuck')`
直接通過定義為匿名函數(shù)的變量名稱來(lái)調(diào)用
echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world');`
使用use
$message = 'hello'; $example = function() use ($message){ var_dump($message); }; echo $example(); //輸出hello $message = 'world'; //輸出hello 因?yàn)槔^承變量的值的時(shí)候是函數(shù)定義的時(shí)候而不是 函數(shù)被調(diào)用的時(shí)候 echo $example(); //重置為hello $message = 'hello'; //此處傳引用 $example = function() use(&$message){ var_dump($message); }; echo $example(); //輸出hello $message = 'world'; echo $example(); //此處輸出world //閉包函數(shù)也用于正常的傳值 $message = 'hello'; $example = function ($data) use ($message){ return "{$data},{$message}"; }; echo $example('world');
example
class Cart{ //在類里面定義常量用 const 關(guān)鍵字,而不是通常的 define() 函數(shù)。 const PRICE_BUTTER = 1.00; const PRICE_MILK = 3.00; const PRICE_EGGS = 6.95; protected $products = []; 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.0; $callback = function($quantity,$product) use ($tax , &$total){ //constant 返回常量的值 //__class__返回類名 $price = constant(__CLASS__."::PRICE_".strtoupper($product)); $total += ($price * $quantity)*($tax+1.0); }; //array_walk() 函數(shù)對(duì)數(shù)組中的每個(gè)元素應(yīng)用用戶自定義函數(shù)。在函數(shù)中,數(shù)組的鍵名和鍵值是參數(shù) array_walk($this->products,$callback); //回調(diào)匿名函數(shù) return round($total,2); } } $my_cart = new Cart(); $my_cart->add('butter',1); $my_cart->add('milk',3); $my_cart->add('eggs',6); print($my_cart->getTotal(0.05));
以上就是關(guān)于PHP閉包函數(shù)的相關(guān)內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
PHP應(yīng)用代碼復(fù)雜度檢測(cè)使用方法
這篇文章主要為大家介紹了PHP應(yīng)用代碼復(fù)雜度檢測(cè)使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06PHP 實(shí)現(xiàn) WebSocket 協(xié)議原理與應(yīng)用詳解
這篇文章主要介紹了PHP 實(shí)現(xiàn) WebSocket 協(xié)議,結(jié)合具體實(shí)例形式較為詳細(xì)的分析了websocket協(xié)議原理、以及PHP具體應(yīng)用相關(guān)操作技巧,需要的朋友可以參考下2020-04-04php小技巧 把數(shù)組的鍵和值交換形成了新的數(shù)組,查找值取得鍵
php小技巧--把數(shù)組的鍵和值交換形成了新的數(shù)組,查找值取得鍵的實(shí)現(xiàn)方法。2011-06-06php echo()和print()、require()和include()函數(shù)區(qū)別說(shuō)明
簡(jiǎn)單總結(jié)echo()和print()、require()和include()等易混淆函數(shù)的區(qū)別2010-03-03PHP 函數(shù)call_user_func和call_user_func_array用法詳解
下面來(lái)和大家分享一下這個(gè)call_user_func_array和call_user_func函數(shù)的用法,另外附贈(zèng)func_get_args()函數(shù)和func_num_args()函數(shù),嘿嘿!!2014-03-03php使用ob_start()實(shí)現(xiàn)圖片存入變量的方法
這篇文章主要介紹了php使用ob_start()實(shí)現(xiàn)圖片存入變量的方法,是對(duì)緩存的靈活運(yùn)用,具有既定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11WordPress中重置文章循環(huán)的rewind_posts()函數(shù)講解
這篇文章主要介紹了WordPress中的文章循環(huán)重置函數(shù)rewind_posts()講解,附帶不依賴循環(huán)的single_cat_title()函數(shù)的用法介紹,需要的朋友可以參考下2016-01-01