詳解PHP匿名函數(shù)與注意事項(xiàng)

字號:


    php5.3不但引進(jìn)了匿名函數(shù)還有更多更好多新的特性了,下面我們一起來了解一下PHP匿名函數(shù)與注意事項(xiàng),具體內(nèi)容如下
    PHP5.2 以前:autoload, PDO 和 MySQLi, 類型約束
    PHP5.2:JSON 支持
    PHP5.3:棄用的功能,匿名函數(shù),新增魔術(shù)方法,命名空間,后期靜態(tài)綁定,Heredoc 和 Nowdoc, const, 三元運(yùn)算符,Phar
    PHP5.4:Short Open Tag, 數(shù)組簡寫形式,Traits, 內(nèi)置 Web 服務(wù)器,細(xì)節(jié)修改
    PHP5.5:yield, list() 用于 foreach, 細(xì)節(jié)修改
    PHP5.6:常量增強(qiáng),可變函數(shù)參數(shù),命名空間增強(qiáng)
    現(xiàn)在基本上都使用PHP5.3以后的版本,但是感覺普遍一個(gè)現(xiàn)象就是很多新特性,過了這么長時(shí)間,還沒有完全普及,在項(xiàng)目中很少用到。 
    看看PHP匿名函數(shù):
    'test' => function(){
      return 'test'
    },
    PHP匿名函數(shù)的定義很簡單,就是給一個(gè)變量賦值,只不過這個(gè)值是個(gè)function。
    以上是使用Yii框架配置components文件,加了一個(gè)test的配置。
    什么是PHP匿名函數(shù)?
    看官方解釋:
    匿名函數(shù)(Anonymous functions),也叫閉包函數(shù)(closures),允許 臨時(shí)創(chuàng)建一個(gè)沒有指定名稱的函數(shù)。最經(jīng)常用作回調(diào)函數(shù)(callback)參數(shù)的值。當(dāng)然,也有其它應(yīng)用的情況。
    匿名函數(shù)示例
    <?php
    echo preg_replace_callback('~-([a-z])~', function ($match) {
     return strtoupper($match[1]);
    }, 'hello-world');
    // 輸出 helloWorld
    ?>
    閉包函數(shù)也可以作為變量的值來使用。PHP 會自動把此種表達(dá)式轉(zhuǎn)換成內(nèi)置類 Closure 的對象實(shí)例。把一個(gè) closure 對象賦值給一個(gè)變量的方式與普通變量賦值的語法是一樣的,最后也要加上分號:
    匿名函數(shù)變量賦值示例
    <?php
    $greet = function($name)
    {
     printf("Hello %s\r\n", $name);
    };
    $greet('World');
    $greet('PHP');
    ?>
    閉包可以從父作用域中繼承變量。 任何此類變量都應(yīng)該用 use 語言結(jié)構(gòu)傳遞進(jìn)去。
    從父作用域繼承變量
    <?php
    $message = 'hello'
    // 沒有 "use"
    $example = function () {
     var_dump($message);
    };
    echo $example();
    // 繼承 $message
    $example = function () use($message) {
     var_dump($message);
    };
    echo $example();
    // Inherited variable's 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");
    ?>
    php中的匿名函數(shù)的注意事項(xiàng)
    在php5.3以后,php加入匿名函數(shù)的使用,今天在使用匿名的時(shí)候出現(xiàn)錯(cuò)誤,不能想php函數(shù)那樣聲明和使用,詳細(xì)看代碼
    $callback=function(){ 
     return "aa"; 
    }; 
    echo $callback(); 
    打印出來是aa;
    看下面的例子:
    echo $callback(); 
    $callback=function(){ 
     return "aa"; 
    }; 
    這時(shí)報(bào)錯(cuò)了!$callback為未聲明,但是使用php自己聲明的函數(shù)都不會報(bào)錯(cuò)的!
    function callback(){ 
     return "aa"; 
    } 
    echo callback(); //aa 
    echo callback(); //aa 
    function callback(){ 
     return "aa"; 
    } 
    這兩個(gè)都打印出來aa;
    在使用匿名函數(shù)的時(shí)候,匿名函數(shù)當(dāng)做變量,須提前聲明,js中也是這樣的?。。。。?BR>    以上就是為大家介紹的PHP匿名函數(shù)與注意事項(xiàng),希望對大家的學(xué)習(xí)有所幫助。