Home  >  Article  >  PHP Framework  >  ThinkPHP container registration tree mode

ThinkPHP container registration tree mode

咔咔
咔咔Original
2020-10-10 11:24:011604browse

Before learning the container source code, you must learn another design pattern, the registration tree pattern. This design pattern is also a core of the container.

2. Registration tree mode

Why do we say this registration tree mode here, because in the framework The mid-registration tree pattern is a dominant position, so you must understand it!

So what is a registration demonstration!

  • The registration tree mode is to register the object instance to a tree (the tree here is not a real tree! It is registered to a global attribute)
  • Then the corresponding object instance can be obtained from the global tree through internal methods.

It definitely won’t be better understood if you say this. Next, Kaka will show you a simple case to have a simple understanding.

A registration tree mode requires four things: the pool of the registration tree, mounting the object to the registration pool, obtaining the object from the registration pool, and unloading the object from the registration pool.

The picture below is a simple registration tree model written by Kaka.

If you don’t understand the code, you need to go and make up the basics!

ThinkPHP container registration tree modeThinkPHP container registration tree modeNext create a TestTree file in the same directory

ThinkPHP container registration tree modeCome to the controller to test whether there is any problem with the registration tree mode written.

Be sure to pay attention to the namespace issue when testing! The kaka directory here was previously configured in the automatic loading of classes. If you don’t know how to do it, you can check it out in the first issue of the article.

This is equivalent to instantiating the TestTree class first

Then use the registration tree mode to register this instance into the object tree pool

Finally use the get method to register this instance Once the class is obtained, you can directly call the methods in TestTree.

ThinkPHP container registration tree modeFinally, take a look at the final print result. The result is the return value of the getTreeContent method in the TestTree class.

ThinkPHP container registration tree modeThe registration tree mode is what Kaka explained above. It means that we don’t have to study the source code. These contents are also what we must learn to use.

Persistence in learning, persistence in blogging, and persistence in 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 registration tree mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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