Home > PHP Framework > ThinkPHP > body text

ThinkPHP container uses design patterns and reflection to implement a simple case

咔咔
Release: 2020-10-10 11:47:21
Original
1575 people have browsed it

This article will use two design patterns and reflection knowledge to implement a simple case, and simply integrate and connect the previously learned knowledge points.

5. Integrating design patterns and reflection to implement a case

Experienced ninety-nine and eighty-one years The difficulty finally comes to the container. In this link, we first implement a container of our own and connect the singleton mode, registration tree mode, and reflection explained before in series, so as to deepen our impression and better understand it.

I still remember that I mentioned such a method in dependency injection before dependency. This method is to perform dependency injection to decouple the code.

ThinkPHP container uses design patterns and reflection to implement a simple caseBut this time! Containers will be used to solve this problem.

First define the required classes. This class uses singleton mode and registration tree mode. The previous articles are not very good, so be sure to read them carefully, otherwise it will be difficult to understand the following articles. of.

ThinkPHP container uses design patterns and reflection to implement a simple case
Insert picture description here

ThinkPHP container uses design patterns and reflection to implement a simple caseThinkPHP container uses design patterns and reflection to implement a simple case

<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>
Copy after login

For the convenience of future viewing, here are the case demonstrations of each section Place it in the corresponding controller

Here transplant the previous dependency injection code and configure annotation routing for access to see if the final result is 123 returned by the Car method

ThinkPHP container uses design patterns and reflection to implement a simple caseTest the printing result, everything is ok

ThinkPHP container uses design patterns and reflection to implement a simple caseThis code was modified using singleton mode and registration tree mode

After modification, the result is printed, which is also returned by car Value 123.

It should be noted here that the set and get methods will not coexist in the same method. They are written together just to give you a demonstration.

When you look at the source code of the container later, you will know how to use the set and get methods. Here we just let you experience the singleton mode and the registration tree mode.

ThinkPHP container uses design patterns and reflection to implement a simple caseMake a small modification here, modify the last two lines of code above

ThinkPHP container uses design patterns and reflection to implement a simple case
Insert picture description here
ThinkPHP container uses design patterns and reflection to implement a simple case
Insert picture description here

Keep learning and persist Writing blogs and insisting on sharing are the beliefs that Kaka has always adhered to since its beginning. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I’m Kaka, see you next time.

The above is the detailed content of ThinkPHP container uses design patterns and reflection to implement a simple case. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!