Home > PHP Framework > ThinkPHP > body text

ThinkPHP plays with its own container class

咔咔
Release: 2020-10-12 13:57:59
Original
1895 people have browsed it
"

This article will implement a simple container class

"

five , Play with your own container class

At this time we modify the Person file

Add a constructor and assign the parameters using the constructor. In the buy method, there is no need to pass parameters, just use this->obj.

ThinkPHP plays with its own container classIf you still run the dependency route directly at this time, the following error will be reported. That is because the constructor in Person has a parameter, but we did not pass it.

ThinkPHP plays with its own container classAt this point, you need to modify one thing, which is to pass the Car instance as a parameter when instantiating the Person. There will be no problem.

ThinkPHP plays with its own container class
Insert picture description here

But you will find out what code is above. What was originally a simple few lines of code has become complicated like this. , this time has already done more harm than good. No matter how good the design pattern is, blind use is also a burden to the project.

So this is when reflection comes. Reflection is briefly introduced above, so be sure to read it! The articles are all linked together.

Reflection Battle Optimization Code

The final optimized code is like this. Next, we will briefly analyze this code.

  • Based on the previous code, only the get method in kaka/container/Container.php was modified
  • Determine whether the named person is in the container
  • Use the reflection interface, and then get the constructor of the person class passed in
  • If person is not The constructor method can directly return the instance of person
  • If person is stored in the constructor, get the method of person constructor
  • Because person The parameters of the constructor in the class are not limited to one
  • , so you need to loop to get the object of each parameter
  • Finally use reflection The newInstanceArgs interface creates the corresponding instance
<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-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br/>     * User : 咔咔<br/>     * Notes: 获取容器里边的实例  使用反射<br/>     * Time :2020/9/21 22:04<br/>     * <span class="hljs-doctag" style="color: #c678dd; line-height: 26px;">@param</span> $key<br/>     * <span class="hljs-doctag" style="color: #c678dd; line-height: 26px;">@return</span> mixed<br/>     */</span><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;">if</span>(!<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">empty</span>(<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">$this</span>->instances[$key])){<br/>            $key = <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">$this</span>->instances[$key];<br/>        }<br/><br/>        $reflect = <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">new</span> \ReflectionClass($key);<br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 获取类的构造函数</span><br/>        $c = $reflect->getConstructor();<br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span>(!$c){<br/>            <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">new</span> $key;<br/>        }<br/><br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 获取构造函数的参数</span><br/>        $params = $c->getParameters();<br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">foreach</span> ($params <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> $param) {<br/>       <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br/>             ReflectionClass Object<br/>            (<br/>                [name] => container\dependency\Car<br/>            )<br/>             */</span><br/>            $class = $param->getClass();<br/>            <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span>(!$class){<br/><br/>            }<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">else</span>{<br/>                <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// container\dependency\Car</span><br/>                $args[] = <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">$this</span>->get($class->name);<br/>            }<br/>        }<br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 从给出的参数创建一个新的类实例</span><br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> $reflect->newInstanceArgs($args);<br/>    }<br/>}<br/></code>
Copy after login

ThinkPHP plays with its own container classFileapplication/index/controller/Container.phpHere are the changes after the modification

ThinkPHP plays with its own container classQuestion 1: kaka/container/dependency/Person.phpWhat does the parameter Car inside mean?

This question is actually very simple, you can see This Car is the Car.php file in the same directory. You can directly understand it as files under the same namespace.

ThinkPHP plays with its own container classQuestion 2: Fileapplication/index/controller/Container.phpWhy can you call the buy method directly

First look at the value of obj, The Car class has already been instantiated in the returned object, so there is no need to instantiate it. You can call the buy method directly because the parameters will be passed directly.

ThinkPHP plays with its own container class
Insert picture here Description

ThinkPHP plays with its own container classThe above is a simple container implemented by Kaka. If you don’t understand or have any questions, you can reply directly in the comment area.

The next step is to analyze the containers in the framework and trace them back to the root cause step by step.

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always upheld since his career. I hope that Kaka’s articles in the huge Internet can bring you a little Silk help. My name is Kaka, see you next time.

The above is the detailed content of ThinkPHP plays with its own container class. 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!