PHP|PHP實踐-閉包

閉包和匿名函數在PHP5.3.0中引入的。php

閉包是指:建立時封裝周圍狀態的函數。即便閉包所處的環境不存在了,閉包中封裝的狀態依然存在。html

理論上,閉包和匿名函數是不一樣的概念。可是PHP將其視做相同概念。json

實際上,閉包和匿名函數是假裝成函數的對象。他們是Closure類的實例。閉包

閉包和字符串、整數同樣,是一等值類型。app

建立閉包

<?php
$clousre = function ($name) {
    return 'Hello ' . $name;
};

echo $closure('nesfo');

咱們之因此能調用$closure變量,是由於這個變量的值是一個閉包,並且閉包對象實現了__invoke()魔術方法。只要變量名後有(),PHP就會查找並調用__invoke()方法。框架

一般會把PHP閉包看成函數的回調使用。函數

array_map(), preg_replace_callback()方法都會用到回調函數,這是使用閉包的最佳時機!this

舉個例子:spa

<?php
$numbersPlusOne = array_map(function ($number) {
    return $number + 1;
}, [1, 2, 3]);
print_r($numbersPlusOne);

獲得結果:code

[2, 3, 4]

在閉包出現以前,只能單首創建具名函數,而後使用名稱引用那個函數。這麼作,代碼執行會稍微慢點,並且把回調的實現和使用場景隔離了。

<?php
function incrementNum ($number) {
    return $number + 1;
}

$numbersPlusOne = array_map('incrementNum', [1, 2, 3]);
print_r($numbersPlusOne);

附加狀態

匿名函數不止能夠當回調使用,還能夠爲PHP附加並封裝狀態。

PHP中,必須手動調用閉包對象的bindTo()方法或者使用use關鍵字,才能把狀態附加到PHP閉包上。

<?php
function enclosePerson ($name) {
    return function ($doCommand) use ($name) {
        return $name . ', ' . $doCommand;
    }
}

$clay = enclosePerson('Clay');

echo $clay('get me sweet tea!');

獲得結果:

"Clay, get me sweet tea!"

PHP閉包是對象,每一個閉包實例均可以使用$this關鍵字獲取閉包的內部狀態。閉包對象的默認狀態沒什麼用,只有__invoke()方法和bindTo方法而已。

咱們可使用bindTo()這個方法,將Closure對象的內部狀態綁定到其它對象上。

bindTo()方法的第二個參數:其做用是指定綁定閉包的那個對象所屬的PHP類。所以,閉包能夠訪問綁定閉包的對象中受保護和私有的成員。

PHP框架常常使用bindTo()方法把路由URL映射到匿名回調函數上。這麼作能夠在這個匿名函數中使用$this關鍵字引用重要的應用對象。

使用bindTo()方法附加閉包狀態

<?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
$app = new App();
$app->addRoute('/user/nesfo', function () {
    $this->responseContentType = 'application/json; charset=utf8';
    $this->responseBody = '{"name": "nesfo"}';
});
$app->dispatch('/user/nesfo');

參考

  1. Modern PHP

相關文章
相關標籤/搜索