閉包
成都創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為靈川等服務(wù)建站,靈川等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為靈川企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
閉包和匿名函數(shù)在PHP5.3.0
中引入的。
閉包是指:
創(chuàng)建時封裝周圍狀態(tài)的函數(shù)。即使閉包所處的環(huán)境不存在了,閉包中封裝的狀態(tài)依然存在。
理論上,閉包和匿名函數(shù)是不同的概念。但是PHP將其視作相同概念。
實際上,閉包和匿名函數(shù)是偽裝成函數(shù)的對象。他們是Closure
類的實例。
閉包和字符串、整數(shù)一樣,是一等值類型。
創(chuàng)建閉包:
<?php $closure = function ($name) { return 'Hello ' . $name; }; echo $closure('nesfo');//Hello nesfo var_dump(method_exists($closure, '__invoke'));//true
我們之所以能調(diào)用$closure
變量,是因為這個變量的值是一個閉包,而且閉包對象實現(xiàn)了__invoke()
魔術(shù)方法。只要變量名后有(),PHP就會查找并調(diào)用__invoke()
方法。
通常會把PHP閉包當(dāng)作函數(shù)的回調(diào)使用。
array_map()
, preg_replace_callback()
方法都會用到回調(diào)函數(shù),這是使用閉包的最佳時機(jī)!
舉個例子:
<?php $numbersPlusOne = array_map(function ($number) { return $number + 1; }, [1, 2, 3]); print_r($numbersPlusOne);
得到結(jié)果:
[2, 3, 4]
在閉包出現(xiàn)之前,只能單獨創(chuàng)建具名函數(shù),然后使用名稱引用那個函數(shù)。這么做,代碼執(zhí)行會稍微慢點,而且把回調(diào)的實現(xiàn)和使用場景隔離了。
<?php function incrementNum ($number) { return $number + 1; } $numbersPlusOne = array_map('incrementNum', [1, 2, 3]); print_r($numbersPlusOne);
SPL
ArrayAccess
實現(xiàn)ArrayAccess
接口,可以使得object像array那樣操作。ArrayAccess接口包含四個必須實現(xiàn)的方法:
interface ArrayAccess { //檢查一個偏移位置是否存在 public mixed offsetExists ( mixed $offset ); //獲取一個偏移位置的值 public mixed offsetGet( mixed $offset ); //設(shè)置一個偏移位置的值 public mixed offsetSet ( mixed $offset ); //復(fù)位一個偏移位置的值 public mixed offsetUnset ( mixed $offset ); }
SplObjectStorage
SplObjectStorage
類實現(xiàn)了以對象為鍵的映射(map)或?qū)ο蟮募希ㄈ绻雎宰鳛殒I的對象所對應(yīng)的數(shù)據(jù))這種數(shù)據(jù)結(jié)構(gòu)。這個類的實例很像一個數(shù)組,但是它所存放的對象都是唯一。該類的另一個特點是,可以直接從中刪除指定的對象,而不需要遍歷或搜索整個集合。
::class
語法
因為 ::class
表示是字符串。用 ::class
的好處在于 IDE 里面可以直接改名一個 class
,然后 IDE 自動處理相關(guān)引用。
同時,PHP 執(zhí)行相關(guān)代碼時,是不會先加載相關(guān) class 的。
同理,代碼自動化檢查 inspect
也可以正確識別 class。
Pimple容器流程淺析
Pimpl是php社區(qū)中比較流行的容器。代碼不是很多,詳見
https://github.com/silexphp/Pimple/blob/master/src/Pimple/Container.php 。
我們的應(yīng)用可以基于Pimple開發(fā):
namespace EasyWeChat\Foundation; use Pimple\Container; class Application extends Container { /** * Service Providers. * * @var array */ protected $providers = [ ServiceProviders\ServerServiceProvider::class, ServiceProviders\UserServiceProvider::class ]; /** * Application constructor. * * @param array $config */ public function __construct($config) { parent::__construct(); $this['config'] = function () use ($config) { return new Config($config); }; if ($this['config']['debug']) { error_reporting(E_ALL); } $this->registerProviders(); } /** * Add a provider. * * @param string $provider * * @return Application */ public function addProvider($provider) { array_push($this->providers, $provider); return $this; } /** * Set providers. * * @param array $providers */ public function setProviders(array $providers) { $this->providers = []; foreach ($providers as $provider) { $this->addProvider($provider); } } /** * Return all providers. * * @return array */ public function getProviders() { return $this->providers; } /** * Magic get access. * * @param string $id * * @return mixed */ public function __get($id) { return $this->offsetGet($id); } /** * Magic set access. * * @param string $id * @param mixed $value */ public function __set($id, $value) { $this->offsetSet($id, $value); } }
如何使用我們的應(yīng)用:
$app = new Application([]); $user = $app->user;
之后我們就可以使用$user對象的方法了。我們發(fā)現(xiàn)其實并沒有$this->user這個屬性,但是可以直接使用。主要是這兩個方法起的作用:
public function offsetSet($id, $value){} public function offsetGet($id){}
下面我們將解釋在執(zhí)行這兩句代碼,Pimple做了什么。但在解釋這個之前,我們先看看容器的一些核心概念。
服務(wù)提供者
服務(wù)提供者是連接容器與具體功能實現(xiàn)類的橋梁。服務(wù)提供者需要實現(xiàn)接口ServiceProviderInterface
:
namespace Pimple; /** * Pimple service provider interface. * * @author Fabien Potencier * @author Dominik Zogg */ interface ServiceProviderInterface { /** * Registers services on the given container. * * This method should only be used to configure services and parameters. * It should not get services. * * @param Container $pimple A container instance */ public function register(Container $pimple); }
所有服務(wù)提供者必須實現(xiàn)接口register方法。
我們的應(yīng)用里默認(rèn)有2個服務(wù)提供者:
protected $providers = [ ServiceProviders\ServerServiceProvider::class, ServiceProviders\UserServiceProvider::class ];
以UserServiceProvider
為例,我們看其代碼實現(xiàn):
namespace EasyWeChat\Foundation\ServiceProviders; use EasyWeChat\User\User; use Pimple\Container; use Pimple\ServiceProviderInterface; /** * Class UserServiceProvider. */ class UserServiceProvider implements ServiceProviderInterface { /** * Registers services on the given container. * * This method should only be used to configure services and parameters. * It should not get services. * * @param Container $pimple A container instance */ public function register(Container $pimple) { $pimple['user'] = function ($pimple) { return new User($pimple['access_token']); }; } }
我們看到,該服務(wù)提供者的注冊方法會給容器增加屬性user
,但是返回的不是對象,而是一個閉包。這個后面我再做講解。
服務(wù)注冊
我們在Application
里構(gòu)造函數(shù)里使用$this->registerProviders();
對所有服務(wù)提供者進(jìn)行了注冊:
private function registerProviders() { foreach ($this->providers as $provider) { $this->register(new $provider()); } }
仔細(xì)看,我們發(fā)現(xiàn)這里實例化了服務(wù)提供者,并調(diào)用了容器Pimple的register方法:
public function register(ServiceProviderInterface $provider, array $values = array()) { $provider->register($this); foreach ($values as $key => $value) { $this[$key] = $value; } return $this; }
而這里調(diào)用了服務(wù)提供者的register
方法,也就是我們在上一節(jié)中提到的:注冊方法給容器增加了屬性user
,但返回的不是對象,而是一個閉包。
當(dāng)我們給容器Pimple添加屬性user的同時,會調(diào)用offsetSet($id, $value)
方法:給容器Pimple
的屬性values
、keys分別賦值:
$this->values[$id] = $value; $this->keys[$id] = true;
到這里,我們還沒有實例化真正提供實際功能的類EasyWeChat\User\Usr
。但已經(jīng)完成了服務(wù)提供者的注冊工作。
當(dāng)我們運行到這里:
$user = $app->user;
會調(diào)用offsetGet($id)并進(jìn)行實例化真正的類:
$raw = $this->values[$id]; $val = $this->values[$id] = $raw($this); $this->raw[$id] = $raw; $this->frozen[$id] = true; return $val;
$raw獲取的是閉包:
$pimple['user'] = function ($pimple) { return new User($pimple['access_token']); };
$raw($this)
返回的是實例化的對象User
。也就是說只有實際調(diào)用才會去實例化具體的類。后面我們就可以通過$this['user']
或者$this->user
調(diào)用User
類里的方法了。
以上就是Pimple運行流程淺析(PHP容器)的詳細(xì)內(nèi)容,更多請關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!
網(wǎng)頁標(biāo)題:PHP容器Pimple的運行流程是怎樣的?
本文路徑:http://aaarwkj.com/article48/ihhshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、微信公眾號、外貿(mào)建站、軟件開發(fā)、域名注冊、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)