匿名函數(shù)(Anonymous functions),也叫閉包函數(shù)(closures),允許 臨時創(chuàng)建一個沒有指定名稱的函數(shù)。最經(jīng)常用作回調(diào)函數(shù)(callback)參數(shù)的值。當然,也有其它應(yīng)用的情況。
匿名函數(shù)目前是通過 Closure 類來實現(xiàn)的。
匿名函數(shù)示例
?phpecho preg_replace_callback( ~-([a-z])~ , function ($match) { return strtoupper($match[1]);}, hello-world // 輸出 helloWorld?
閉包函數(shù)也可以作為變量的值來使用。PHP 會自動把此種表達式轉(zhuǎn)換成內(nèi)置類 Closure 的對象實例。把一個 closure 對象賦值給一個變量的方式與普通變量賦值的語法是一樣的,最后也要加上分號:
匿名函數(shù)變量賦值示例
?php$greet = function($name) printf( Hello %s/r/n , $name);$greet( World $greet( PHP ?
閉包可以從父作用域中繼承變量。 任何此類變量都應(yīng)該用 use 語言結(jié)構(gòu)傳遞進去。 PHP 7.1 起,不能傳入此類變量: superglobals、 $this 或者和參數(shù)重名。
從父作用域繼承變量
?php$message = hello // 沒有 use $example = function () { var_dump($message);echo $example();// 繼承 $message$example = function () use ($message) { var_dump($message);echo $example();// Inherited variable s html' target='_blank'>value is from when the function// is defined, not when called$message = world echo $example();// Reset message$message = hello // Inherit by-reference$example = function () use ( $message) { var_dump($message);echo $example();// The changed value in the parent scope// is reflected inside the function call$message = world echo $example();// Closures can also accept regular arguments$example = function ($arg) use ($message) { var_dump($arg . . $message);$example( hello ?
以上例程的輸出類似于:
Notice: Undefined variable: message in /example.php on line 6string(5) hello string(5) hello string(5) hello string(5) world string(11) hello world
這些變量都必須在函數(shù)或類的頭部聲明。 從父作用域中繼承變量與使用全局變量是不同的。全局變量存在于一個全局的范圍,無論當前在執(zhí)行的是哪個函數(shù)。而 閉包的父作用域是定義該閉包的函數(shù)(不一定是調(diào)用它的函數(shù))。
Closures 和作用域示例:
?php// 一個基本的購物車,包括一些已經(jīng)添加的商品和每種商品的數(shù)量。// 其中有一個方法用來計算購物車中所有商品的總價格,該方法使// 用了一個 closure 作為回調(diào)函數(shù)。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) . /n // 最后結(jié)果是 54.29?
本篇文章就是匿名函數(shù)的介紹,希望對需要的朋友有所幫助!
以上就是什么是匿名函數(shù)的詳細內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答