国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

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

2024-09-04 11:49:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

匿名函數(shù)是PHP5.3引進(jìn)來(lái)了,php5.3不但引進(jìn)了匿名函數(shù)還有更多更好多新的特性了,下面我們一起來(lái)了解一下PHP匿名函數(shù)與注意事項(xiàng)詳解

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ù)組簡(jiǎn)寫(xiě)形式,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以后的版本,但是感覺(jué)普遍一個(gè)現(xiàn)象就是很多新特性,過(guò)了這么長(zhǎng)時(shí)間,還沒(méi)有完全普及,在項(xiàng)目中很少用到。

看看PHP匿名函數(shù):

 'test' => function(){
        return 'test'
},

PHP匿名函數(shù)的定義很簡(jiǎn)單,就是給一個(gè)變量賦值,只不過(guò)這個(gè)值是個(gè)function。

以上是使用Yii框架配置components文件,加了一個(gè)test的配置。

在另一個(gè)模板頁(yè)面打印試試:

test ?>//test

OK.

什么是PHP匿名函數(shù)?

看官方解釋:

匿名函數(shù)(Anonymous functions),也叫閉包函數(shù)(closures),允許 臨時(shí)創(chuàng)建一個(gè)沒(méi)有指定名稱的函數(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ù)也可以作為變量的值來(lái)使用。PHP 會(huì)自動(dòng)把此種表達(dá)式轉(zhuǎn)換成內(nèi)置類 Closure 的對(duì)象實(shí)例。把一個(gè) closure 對(duì)象賦值給一個(gè)變量的方式與普通變量賦值的語(yǔ)法是一樣的,最后也要加上分號(hào):

匿名函數(shù)變量賦值示例

<?php

$greet = function($name)
{
    printf("Hello %s/r/n", $name);
};
 
$greet('World');
$greet('PHP');
?>

閉包可以從父作用域中繼承變量。 任何此類變量都應(yīng)該用 use 語(yǔ)言結(jié)構(gòu)傳遞進(jìn)去。

從父作用域繼承變量:

<?php
$message = 'hello'
 
// 沒(méi)有 "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");
?>

以上例程的輸出類似于:

Notice: Undefined variable: message in /example.php on line 6
NULL
string(5) "hello"
string(5) "hello"
string(5) "hello"
string(5) "world"
string(11) "hello world"
 

php中的匿名函數(shù)的注意事項(xiàng)

在php5.3以后,php加入匿名函數(shù)的使用,今天在使用匿名的時(shí)候出現(xiàn)錯(cuò)誤,不能想php函數(shù)那樣聲明和使用,詳細(xì)看代碼

$callback=function(){ 
  return "aa"; 
}; 

echo $callback(); 

這是打印出來(lái)是aa;

看下面的例子:

echo $callback(); 
$callback=function(){ 
  return "aa"; 
}; 

這是報(bào)錯(cuò)了!報(bào)的錯(cuò)誤時(shí):

Notice: Undefined variable: callback in D:/php/www/zf2/public/04.php on line 9
Fatal error: Function name must be a string in D:/php/www/zf2/public/04.php on line 9

$callback為未聲明,

但是使用php自己聲明的函數(shù)都不會(huì)報(bào)錯(cuò)的!

function callback(){ 
  return "aa"; 
echo callback();  //aa 
 
echo callback();  //aa 
function callback(){ 
  return "aa"; 

這兩個(gè)都打印出來(lái)aa;

在使用匿名函數(shù)的時(shí)候,匿名函數(shù)當(dāng)做變量,須提前聲明,js中也是這樣的?。。。?!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 扬中市| 钟山县| 贵定县| 道真| 凤翔县| 藁城市| 库车县| 赫章县| 富裕县| 桐柏县| 平定县| 马尔康县| 怀集县| 买车| 新河县| 漠河县| 浑源县| 昌都县| 榆中县| 息烽县| 安达市| 札达县| 始兴县| 遂昌县| 泰顺县| 自治县| 宝清县| 弥勒县| 洪江市| 万源市| 莱州市| 中超| 密云县| 罗江县| 游戏| 清镇市| 静安区| 贡嘎县| 新巴尔虎右旗| 黄平县| 华宁县|