《PHP實例:PHP 閉包詳解及實例代碼》要點:
本文介紹了PHP實例:PHP 閉包詳解及實例代碼,希望對您有用。如果有疑問,可以聯系我們。
PHP實例閉包和匿名函數在PHP5.3.0中引入的.
PHP實例閉包是指:創建時封裝周圍狀態的函數.即使閉包所處的環境不存在了,閉包中封裝的狀態依然存在.
PHP實例理論上,閉包和匿名函數是不同的概念.但是PHP將其視作相同概念.
PHP實例實際上,閉包和匿名函數是偽裝成函數的對象.他們是Closure類的實例.
PHP實例閉包和字符串、整數一樣,是一等值類型.
PHP實例創建閉包
PHP實例
<?php
$clousre = function ($name) {
return 'Hello ' . $name;
};
echo $closure('nesfo');
PHP實例我們之所以能調用$closure變量,是因為這個變量的值是一個閉包,而且閉包對象實現了__invoke()魔術方法.只要變量名后有(),PHP就會查找并調用__invoke()方法.通常會把PHP閉包當作函數的回調使用.array_map(), preg_replace_callback()方法都會用到回調函數,這是使用閉包的最佳時機!
PHP實例舉個例子:
PHP實例
<?php
$numbersPlusOne = array_map(function ($number) {
return $number + 1;
}, [1, 2, 3]);
print_r($numbersPlusOne);
PHP實例得到結果:
PHP實例[2, 3, 4]
PHP實例在閉包出現之前,只能單獨創建具名函數,然后使用名稱引用那個函數.這么做,代碼執行會稍微慢點,而且把回調的實現和使用場景隔離了.
PHP實例
<?php
function incrementNum ($number) {
return $number + 1;
}
$numbersPlusOne = array_map('incrementNum', [1, 2, 3]);
print_r($numbersPlusOne);
PHP實例附加狀態
PHP實例匿名函數不止可以當回調使用,還可以為PHP附加并封裝狀態.
PHP實例PHP中,必須手動調用閉包對象的bindTo()方法或者使用use關鍵字,才能把狀態附加到PHP閉包上.
PHP實例
<?php
function enclosePerson ($name) {
return function ($doCommand) use ($name) {
return $name . ', ' . $doCommand;
}
}
$clay = enclosePerson('Clay');
echo $clay('get me sweet tea!');
PHP實例得到結果:
PHP實例"Clay, get me sweet tea!"
PHP實例PHP閉包是對象,每個閉包實例都可以使用$this關鍵字獲取閉包的內部狀態.閉包對象的默認狀態沒什么用,只有__invoke()方法和bindTo方法而已.
PHP實例我們可以使用bindTo()這個方法,將Closure對象的內部狀態綁定到其它對象上.
PHP實例bindTo()方法的第二個參數:其作用是指定綁定閉包的那個對象所屬的PHP類.因此,閉包可以訪問綁定閉包的對象中受保護和私有的成員.
PHP實例PHP框架經常使用bindTo()方法把路由URL映射到匿名回調函數上.這么做可以在這個匿名函數中使用$this關鍵字引用重要的應用對象.
PHP實例使用bindTo()方法附加閉包狀態
PHP實例 <?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實例
<?php
$app = new App();
$app->addRoute('/user/nesfo', function () {
$this->responseContentType = 'application/json; charset=utf8';
$this->responseBody = '{"name": "nesfo"}';
});
$app->dispatch('/user/nesfo');
PHP實例以上就是對PHP 閉包資料的資料整理,后續繼續補充相關資料謝謝大家對本站的支持!
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/3181.html