《PHP實(shí)例:PHP 閉包詳解及實(shí)例代碼》要點(diǎn):
本文介紹了PHP實(shí)例:PHP 閉包詳解及實(shí)例代碼,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP實(shí)例閉包和匿名函數(shù)在PHP5.3.0中引入的.
PHP實(shí)例閉包是指:創(chuàng)建時(shí)封裝周?chē)鸂顟B(tài)的函數(shù).即使閉包所處的環(huán)境不存在了,閉包中封裝的狀態(tài)依然存在.
PHP實(shí)例理論上,閉包和匿名函數(shù)是不同的概念.但是PHP將其視作相同概念.
PHP實(shí)例實(shí)際上,閉包和匿名函數(shù)是偽裝成函數(shù)的對(duì)象.他們是Closure類(lèi)的實(shí)例.
PHP實(shí)例閉包和字符串、整數(shù)一樣,是一等值類(lèi)型.
PHP實(shí)例創(chuàng)建閉包
PHP實(shí)例
<?php
$clousre = function ($name) {
return 'Hello ' . $name;
};
echo $closure('nesfo');
PHP實(shí)例我們之所以能調(diào)用$closure變量,是因?yàn)檫@個(gè)變量的值是一個(gè)閉包,而且閉包對(duì)象實(shí)現(xiàn)了__invoke()魔術(shù)方法.只要變量名后有(),PHP就會(huì)查找并調(diào)用__invoke()方法.通常會(huì)把PHP閉包當(dāng)作函數(shù)的回調(diào)使用.array_map(), preg_replace_callback()方法都會(huì)用到回調(diào)函數(shù),這是使用閉包的最佳時(shí)機(jī)!
PHP實(shí)例舉個(gè)例子:
PHP實(shí)例
<?php
$numbersPlusOne = array_map(function ($number) {
return $number + 1;
}, [1, 2, 3]);
print_r($numbersPlusOne);
PHP實(shí)例得到結(jié)果:
PHP實(shí)例[2, 3, 4]
PHP實(shí)例在閉包出現(xiàn)之前,只能單獨(dú)創(chuàng)建具名函數(shù),然后使用名稱(chēng)引用那個(gè)函數(shù).這么做,代碼執(zhí)行會(huì)稍微慢點(diǎn),而且把回調(diào)的實(shí)現(xiàn)和使用場(chǎng)景隔離了.
PHP實(shí)例
<?php
function incrementNum ($number) {
return $number + 1;
}
$numbersPlusOne = array_map('incrementNum', [1, 2, 3]);
print_r($numbersPlusOne);
PHP實(shí)例附加狀態(tài)
PHP實(shí)例匿名函數(shù)不止可以當(dāng)回調(diào)使用,還可以為PHP附加并封裝狀態(tài).
PHP實(shí)例PHP中,必須手動(dòng)調(diào)用閉包對(duì)象的bindTo()方法或者使用use關(guān)鍵字,才能把狀態(tài)附加到PHP閉包上.
PHP實(shí)例
<?php
function enclosePerson ($name) {
return function ($doCommand) use ($name) {
return $name . ', ' . $doCommand;
}
}
$clay = enclosePerson('Clay');
echo $clay('get me sweet tea!');
PHP實(shí)例得到結(jié)果:
PHP實(shí)例"Clay, get me sweet tea!"
PHP實(shí)例PHP閉包是對(duì)象,每個(gè)閉包實(shí)例都可以使用$this關(guān)鍵字獲取閉包的內(nèi)部狀態(tài).閉包對(duì)象的默認(rèn)狀態(tài)沒(méi)什么用,只有__invoke()方法和bindTo方法而已.
PHP實(shí)例我們可以使用bindTo()這個(gè)方法,將Closure對(duì)象的內(nèi)部狀態(tài)綁定到其它對(duì)象上.
PHP實(shí)例bindTo()方法的第二個(gè)參數(shù):其作用是指定綁定閉包的那個(gè)對(duì)象所屬的PHP類(lèi).因此,閉包可以訪問(wèn)綁定閉包的對(duì)象中受保護(hù)和私有的成員.
PHP實(shí)例PHP框架經(jīng)常使用bindTo()方法把路由URL映射到匿名回調(diào)函數(shù)上.這么做可以在這個(gè)匿名函數(shù)中使用$this關(guān)鍵字引用重要的應(yīng)用對(duì)象.
PHP實(shí)例使用bindTo()方法附加閉包狀態(tài)
PHP實(shí)例 <?php class App { protected $routes = []; protected $responseStatus = '200 OK'; protected $responseContentType = 'text/html'; protected $responseBody = 'Hello world'; public function addRoute($routePath, $routeCallback){ $this->routes[$routePath] = $routeCallback->bindTo($this, __CLASS__); } public function dispatch($currentPath){ foreach($this->routes as $routePath => $callback){ if ($routePath === $currentPath) { $callback(); } } header('HTTP/1.1' . $this->responseStatus); header('Content-type: ' . $this->responseContentType); header('Content-length' . mb_strlen($this->responseBody)); echo $this->responseBody; } }
PHP實(shí)例
<?php
$app = new App();
$app->addRoute('/user/nesfo', function () {
$this->responseContentType = 'application/json; charset=utf8';
$this->responseBody = '{"name": "nesfo"}';
});
$app->dispatch('/user/nesfo');
PHP實(shí)例以上就是對(duì)PHP 閉包資料的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料謝謝大家對(duì)本站的支持!
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/3181.html