搜尋
首頁php框架ThinkPHPThinkPHP容器之使用設計模式和反射實現一個簡單的案例

本文將使用兩個種設計模式和反射知識實作一個簡單的案例,把之前學習過的知識點進行簡單的融合串連起來。

五、融合設計模式與反射實現一個案例

經歷了九九八十一難終於來到了容器這一環節,在這一環節我們先來實現一個自己的容器,將之前講解的單例模式、註冊樹模式、反射進行一個串聯,從而進行加深印象和更好的理解。

還記得之前在依賴注入裡邊說過這樣一個方法dependency,這個方法就是進行了依賴注入,從而對程式碼進行解耦。

ThinkPHP容器之使用設計模式和反射實現一個簡單的案例但這次呢!會使用容器來解決這個問題。

先把需要的類別定義好,這一類別就使用了單例模式和註冊樹模式,之前的文章沒有好好看的,一定要仔細看一下,否則後文會很難理解的。

ThinkPHP容器之使用設計模式和反射實現一個簡單的案例
在這裡插入圖片描述

ThinkPHP容器之使用設計模式和反射實現一個簡單的案例ThinkPHP容器之使用設計模式和反射實現一個簡單的案例

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;"><span class="hljs-meta" style="color: #61aeee; line-height: 26px;"><?php</span><br/><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br/> * Created by PhpStorm.<br/> * User: 咔咔<br/> * Date: 2020/9/21<br/> * Time: 19:04<br/> */</span><br/><br/><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">namespace</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">container</span>;<br/><br/><br/><span class="hljs-class" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">class</span> <span class="hljs-title" style="color: #e6c07b; line-height: 26px;">Container</span><br/></span>{<br/>    <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br/>     * 存放容器<br/>     * <span class="hljs-doctag" style="color: #c678dd; line-height: 26px;">@var</span> array<br/>     */</span><br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">public</span> $instances = [];<br/><br/>    <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br/>     * 容器的对象实例<br/>     * <span class="hljs-doctag" style="color: #c678dd; line-height: 26px;">@var</span> array<br/>     */</span><br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">protected</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">static</span> $instance;<br/><br/>    <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br/>     * 定义一个私有的构造函数防止外部类实例化<br/>     * Container constructor.<br/>     */</span><br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">private</span> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">__construct</span><span class="hljs-params" style="line-height: 26px;">()</span> </span>{<br/><br/>    }<br/><br/>    <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br/>     * 获取当前容器的实例(单例模式)<br/>     * <span class="hljs-doctag" style="color: #c678dd; line-height: 26px;">@return</span> array|Container<br/>     */</span><br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">public</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">static</span> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">getInstance</span> <span class="hljs-params" style="line-height: 26px;">()</span><br/>    </span>{<br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span>(is_null(<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">self</span>::$instance)){<br/>            <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">self</span>::$instance = <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">new</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">self</span>();<br/>        }<br/><br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">self</span>::$instance;<br/>    }<br/><br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">public</span> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">set</span> <span class="hljs-params" style="line-height: 26px;">($key,$value)</span><br/>    </span>{<br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">$this</span>->instances[$key] = $value;<br/>    }<br/><br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">public</span> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> <span class="hljs-title" style="color: #61aeee; line-height: 26px;">get</span> <span class="hljs-params" style="line-height: 26px;">($key)</span><br/>    </span>{<br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">$this</span>->instances[$key];<br/>    }<br/>}<br/></code>

為了方便以後查看方便,這裡把每節的案例演示都放在對應的控制器中

這裡把之前的依賴注入的程式碼移植過來,並且配置上註解路由進行訪問,看最終結果是否為Car方法返回的123

ThinkPHP容器之使用設計模式和反射實現一個簡單的案例測試一下列印結果,一切ok

ThinkPHP容器之使用設計模式和反射實現一個簡單的案例使用單例模式和註冊樹模式配合後修改的這份程式碼

修改後列印出其結果,同樣也是car回傳的值123。

在這裡要注意一下就是在同一個方法中set和get方法是不會共存的,這裡只是為了給大家做一個演示寫在一起的。

後邊在看容器原始碼時就知道set和get方法到底是怎麼使用的,這裡只是讓大家體驗一下單例模式和註冊樹模式。

ThinkPHP容器之使用設計模式和反射實現一個簡單的案例這裡做一個小修改,修改上文最後倆行程式碼

ThinkPHP容器之使用設計模式和反射實現一個簡單的案例
在這裡插入圖片描述
ThinkPHP容器之使用設計模式和反射實現一個簡單的案例
在這裡插入圖片描述

堅持學習、堅持寫博、堅持分享是咔咔從業以來一直所秉持的信念。希望在偌大互聯網中咔咔的文章能帶給你一絲絲幫助。我是喀喀,下期見。

#

以上是ThinkPHP容器之使用設計模式和反射實現一個簡單的案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
ThinkPHP內置測試框架的關鍵功能是什麼?ThinkPHP內置測試框架的關鍵功能是什麼?Mar 18, 2025 pm 05:01 PM

本文討論了ThinkPHP的內置測試框架,突出了其關鍵功能(例如單元和集成測試),以及它如何通過早期的錯誤檢測和改進的代碼質量來增強應用程序可靠性。

如何使用ThinkPHP來構建實時股票市場數據源?如何使用ThinkPHP來構建實時股票市場數據源?Mar 18, 2025 pm 04:57 PM

文章討論了使用ThinkPHP進行實時股票市場數據提要,重點是設置,數據準確性,優化和安全措施。

在無服務器體系結構中使用ThinkPHP的關鍵注意事項是什麼?在無服務器體系結構中使用ThinkPHP的關鍵注意事項是什麼?Mar 18, 2025 pm 04:54 PM

本文討論了在無服務器體系結構中使用ThinkPHP的關鍵注意事項,專注於性能優化,無狀態設計和安全性。它突出了諸如成本效率和可擴展性之類的收益,但也應對挑戰

如何在ThinkPHP微服務中實現服務發現和負載平衡?如何在ThinkPHP微服務中實現服務發現和負載平衡?Mar 18, 2025 pm 04:51 PM

本文討論了在ThinkPHP微服務中實施服務發現和負載平衡,重點是設置,最佳實踐,集成方法和推薦工具。[159個字符]

ThinkPHP依賴性注入容器的高級功能是什麼?ThinkPHP依賴性注入容器的高級功能是什麼?Mar 18, 2025 pm 04:50 PM

ThinkPHP的IOC容器提供了高級功能,例如懶惰加載,上下文綁定和方法注入PHP App中有效依賴性管理的方法。Character計數:159

如何使用ThinkPHP來構建實時協作工具?如何使用ThinkPHP來構建實時協作工具?Mar 18, 2025 pm 04:49 PM

本文討論了使用ThinkPHP來構建實時協作工具,重點關注設置,Websocket集成和安全性最佳實踐。

使用ThinkPHP來構建SaaS應用程序的主要好處是什麼?使用ThinkPHP來構建SaaS應用程序的主要好處是什麼?Mar 18, 2025 pm 04:46 PM

ThinkPHP具有輕巧的設計,MVC架構和可擴展性。它通過各種功能提高可擴展性,加快開發並提高安全性。

如何使用ThinkPHP和RabbitMQ構建分佈式任務隊列系統?如何使用ThinkPHP和RabbitMQ構建分佈式任務隊列系統?Mar 18, 2025 pm 04:45 PM

本文概述了使用ThinkPhp和RabbitMQ構建分佈式任務隊列系統,重點是安裝,配置,任務管理和可擴展性。關鍵問題包括確保高可用性,避免常見的陷阱,例如不當

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。