摘要:實(shí)際上,閉包和匿名函數(shù)是偽裝成函數(shù)的對象。容器流程淺析是社區(qū)中比較流行的容器。服務(wù)提供者服務(wù)提供者是連接容器與具體功能實(shí)現(xiàn)類的橋梁。服務(wù)提供者需要實(shí)現(xiàn)接口所有服務(wù)提供者必須實(shí)現(xiàn)接口方法。但已經(jīng)完成了服務(wù)提供者的注冊工作。
需要具備的知識點(diǎn) 閉包
閉包和匿名函數(shù)在PHP5.3.0中引入的。
閉包是指:創(chuàng)建時(shí)封裝周圍狀態(tài)的函數(shù)。即使閉包所處的環(huán)境不存在了,閉包中封裝的狀態(tài)依然存在。
理論上,閉包和匿名函數(shù)是不同的概念。但是PHP將其視作相同概念。
實(shí)際上,閉包和匿名函數(shù)是偽裝成函數(shù)的對象。他們是Closure類的實(shí)例。
閉包和字符串、整數(shù)一樣,是一等值類型。
創(chuàng)建閉包:
我們之所以能調(diào)用$closure變量,是因?yàn)檫@個(gè)變量的值是一個(gè)閉包,而且閉包對象實(shí)現(xiàn)了__invoke()魔術(shù)方法。只要變量名后有(),PHP就會查找并調(diào)用__invoke()方法。通常會把PHP閉包當(dāng)作函數(shù)的回調(diào)使用。
array_map(), preg_replace_callback()方法都會用到回調(diào)函數(shù),這是使用閉包的最佳時(shí)機(jī)!
舉個(gè)例子:
得到結(jié)果:
[2, 3, 4]在閉包出現(xiàn)之前,只能多帶帶創(chuàng)建具名函數(shù),然后使用名稱引用那個(gè)函數(shù)。這么做,代碼執(zhí)行會稍微慢點(diǎn),而且把回調(diào)的實(shí)現(xiàn)和使用場景隔離了。
SPL ArrayAccess實(shí)現(xiàn)ArrayAccess接口,可以使得object像array那樣操作。ArrayAccess接口包含四個(gè)必須實(shí)現(xiàn)的方法:
interface ArrayAccess { //檢查一個(gè)偏移位置是否存在 public mixed offsetExists ( mixed $offset ); //獲取一個(gè)偏移位置的值 public mixed offsetGet( mixed $offset ); //設(shè)置一個(gè)偏移位置的值 public mixed offsetSet ( mixed $offset ); //復(fù)位一個(gè)偏移位置的值 public mixed offsetUnset ( mixed $offset ); }SplObjectStorageSplObjectStorage類實(shí)現(xiàn)了以對象為鍵的映射(map)或?qū)ο蟮募希ㄈ绻雎宰鳛殒I的對象所對應(yīng)的數(shù)據(jù))這種數(shù)據(jù)結(jié)構(gòu)。這個(gè)類的實(shí)例很像一個(gè)數(shù)組,但是它所存放的對象都是唯一。該類的另一個(gè)特點(diǎn)是,可以直接從中刪除指定的對象,而不需要遍歷或搜索整個(gè)集合。
::class語法因?yàn)?::class 表示是字符串。用 ::class 的好處在于 IDE 里面可以直接改名一個(gè) class,然后 IDE 自動處理相關(guān)引用。
同時(shí),PHP 執(zhí)行相關(guān)代碼時(shí),是不會先加載相關(guān) class 的。同理,代碼自動化檢查 inspect 也可以正確識別 class。
Pimple容器流程淺析Pimpl是php社區(qū)中比較流行的容器。代碼不是很多,詳見https://github.com/silexphp/P... 。
我們的應(yīng)用可以基于Pimple開發(fā):
namespace EasyWeChatFoundation; use PimpleContainer; class Application extends Container { /** * Service Providers. * * @var array */ protected $providers = [ ServiceProvidersServerServiceProvider::class, ServiceProvidersUserServiceProvider::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)其實(shí)并沒有$this->user這個(gè)屬性,但是可以直接使用。主要是這兩個(gè)方法起的作用:
public function offsetSet($id, $value){} public function offsetGet($id){}下面我們將解釋在執(zhí)行這兩句代碼,Pimple做了什么。但在解釋這個(gè)之前,我們先看看容器的一些核心概念。
服務(wù)提供者服務(wù)提供者是連接容器與具體功能實(shí)現(xiàn)類的橋梁。服務(wù)提供者需要實(shí)現(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ù)提供者必須實(shí)現(xiàn)接口register方法。
我們的應(yīng)用里默認(rèn)有2個(gè)服務(wù)提供者:
protected $providers = [ ServiceProvidersServerServiceProvider::class, ServiceProvidersUserServiceProvider::class ];以UserServiceProvider為例,我們看其代碼實(shí)現(xiàn):
namespace EasyWeChatFoundationServiceProviders; use EasyWeChatUserUser; use PimpleContainer; use PimpleServiceProviderInterface; /** * 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,但是返回的不是對象,而是一個(gè)閉包。這個(gè)后面我再做講解。
服務(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)這里實(shí)例化了服務(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,但返回的不是對象,而是一個(gè)閉包。
當(dāng)我們給容器Pimple添加屬性user的同時(shí),會調(diào)用offsetSet($id, $value)方法:給容器Pimple的屬性values、keys分別賦值:
$this->values[$id] = $value; $this->keys[$id] = true;到這里,我們還沒有實(shí)例化真正提供實(shí)際功能的類EasyWeChatUserUsr。但已經(jīng)完成了服務(wù)提供者的注冊工作。
當(dāng)我們運(yùn)行到這里:
$user = $app->user;會調(diào)用offsetGet($id)并進(jìn)行實(shí)例化真正的類:
$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)返回的是實(shí)例化的對象User。也就是說只有實(shí)際調(diào)用才會去實(shí)例化具體的類。后面我們就可以通過$this["user"]或者$this->user調(diào)用User類里的方法了。
當(dāng)然,Pimple里還有很多特性值得我們?nèi)ド钊胙芯浚@里不做過多講解。
參考1、PHP: 數(shù)組式訪問 - Manual
http://php.net/manual/zh/clas...
2、利用 SPL 快速實(shí)現(xiàn) Observer 設(shè)計(jì)模式
https://www.ibm.com/developer...
3、Pimple - A simple PHP Dependency Injection Container
https://pimple.sensiolabs.org/
4、Laravel源碼里面為什么要用::class語法? - 知乎
https://www.zhihu.com/questio...
5、Laravel 學(xué)習(xí)筆記 —— 神奇的服務(wù)容器 | Laravel China 社區(qū) - 高品質(zhì)的 Laravel 和 PHP 開發(fā)者社區(qū) - Powered by PHPHub
https://laravel-china.org/top...
6、Pimple/README_zh.rst at master · 52fhy/Pimple
https://github.com/52fhy/Pimp...原文發(fā)布于博客園:http://www.cnblogs.com/52fhy/...歡迎關(guān)注公眾號及時(shí)獲取最新文章推送!
[推薦] PHP進(jìn)階之路
內(nèi)容概要:從億級 PV 項(xiàng)目的架構(gòu)梳理,到性能提升實(shí)戰(zhàn),然后在更大體系的系統(tǒng)下,構(gòu)造并使用服務(wù)治理框架。最后不要拘泥于一門語言,使用 java 快速構(gòu)建一套 api 服務(wù)。包含內(nèi)容:
純干貨!講師是阿里巴巴資深研發(fā)工程師周夢康,《深入 PHP 內(nèi)核》作者之一。感興趣的朋友可以點(diǎn)擊試看!
推薦!每月僅需$2.5,即可擁有配置SSD的VPS!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/23165.html
摘要:服務(wù)通過匿名函數(shù)定義,返回一個(gè)對象的實(shí)例定義一些服務(wù)請注意,匿名函數(shù)可以訪問當(dāng)前容器實(shí)例,從而允許引用其他服務(wù)或參數(shù)。如果要為所有調(diào)用返回不同的實(shí)例,請使用方法包裝你的匿名函數(shù)。 鏈接 官網(wǎng) WebSite GitHub - Pimple 這是 Pimple 3.x 的文檔。如果你正在使用 Pimple 1.x ,請查看 Pimple 1.x 文檔。閱讀 Pimple 1.x ...
摘要:服務(wù)容器接口是的簡寫,由組織制定的規(guī)范,是開發(fā)的實(shí)踐標(biāo)準(zhǔn)。實(shí)現(xiàn)的容器類源碼很簡單,主要是傳入變量,然后設(shè)置這個(gè)兩個(gè)方法。原創(chuàng)文章,歡迎轉(zhuǎn)載。原文鏈接地址作者發(fā)表日期 接著上篇 還有一些內(nèi)容沒有寫,上篇已經(jīng)把關(guān)于 Pimple 最主要的代碼分析了一下,這篇主要是關(guān)于 PSR-11 兼容性的分析。 PSR-11 服務(wù)容器接口 PSR PSR 是 PHP Standard Recommend...
摘要:已經(jīng)有了非常好的的相關(guān)解析,建議先看下一個(gè)簡單的依賴注入容器讀源碼筆記上讀源碼筆記下這里通過例子補(bǔ)充下核心方法的說明相關(guān)的類型服務(wù)類似單例工廠服務(wù)多個(gè)實(shí)例參數(shù)僅僅是保存一些變量保護(hù)參數(shù)匿名函數(shù)都會被認(rèn)為服務(wù),但是如果僅僅是想作為一個(gè) 已經(jīng)有了非常好的Pimple的相關(guān)解析,建議先看下:Pimple - 一個(gè)簡單的 PHP 依賴注入容器讀 PHP - Pimple 源碼筆記(上)讀 PH...
摘要:也就是閑時(shí)為了寫文章而寫的一篇關(guān)于源碼的閱讀筆記。是標(biāo)準(zhǔn)庫的縮寫,一組旨在解決標(biāo)準(zhǔn)問題的接口和類的集合。提供了一套標(biāo)準(zhǔn)的數(shù)據(jù)結(jié)構(gòu),一組遍歷對象的迭代器,一組接口,一組標(biāo)準(zhǔn)的異常,一系列用于處理文件的類,提供了一組函數(shù),具體可以查看文檔。 也就是閑時(shí)為了寫文章而寫的一篇關(guān)于 Pimple 源碼的閱讀筆記。Pimple 代碼有兩種編碼方式,一種是以 PHP 編寫的,另一種是以 C 擴(kuò)展編寫...
摘要:安裝代碼加載并實(shí)例化參數(shù)存儲單例存儲非單例存儲存儲匿名函數(shù)獲取匿名函數(shù)服務(wù)提供者文檔官網(wǎng)文檔 Pimple/Container 安裝 composer require pimple/pimple: ^3.0 代碼 加載并實(shí)例化 require __DIR__ . /vendor/autoload.php; $pc = new PimpleContainer(); 參數(shù)存儲 $pc[ap...
閱讀 3249·2021-11-23 09:51
閱讀 1639·2021-11-22 09:34
閱讀 2924·2021-10-27 14:15
閱讀 2405·2021-10-12 10:17
閱讀 2016·2021-10-12 10:12
閱讀 1035·2021-09-27 14:00
閱讀 2077·2021-09-22 15:19
閱讀 1104·2019-08-30 10:51