Home  >  Article  >  PHP Framework  >  Preliminary analysis of ThinkPHP container

Preliminary analysis of ThinkPHP container

咔咔
咔咔Original
2020-10-12 12:46:282009browse

This article is a simple combing before parsing the source code of the container class. After combing the general combing, you can have an impression in your mind before you can analyze the container in depth.

7. Preliminary analysis of the Container container class

In the previous article I have implemented a container that I created. Next, let’s take a look at the container in the source code. All the technical points that appeared in the container in the previous article have been covered.

It won’t be very difficult to read the container source code next. If you haven’t read the previous article, be sure to read it again!

I believe that everyone has opened a file public/index.php countless times.

How many times have I opened this file and wanted to find out the source code, but I gave up after looking at it.

After going through the previous registration tree mode, you will definitely understand what this line of code will returnContainer::get('app')

This line of code returns an instance of the app, and you can make a simple breakpoint.

You can see that the return is many attributes in the app class.

So the registration tree mode will not continue to go back and read what was written before, otherwise the more you read, the more confused you will become.

The previous technical points are all for the purpose of paving the way for the following. If you don’t read the previous articles, you won’t be able to read the following. Experts won’t talk about it!

So how are the containers in the framework defined? How is it achieved?

That is, you only need to pay attention to what this get() method does.

The code will be traced to the get()method## in the file thinkphp/library/think/Container.php

#The

getInstance() method here is familiar, right? This is the singleton pattern mentioned above. That is to say, only one instance of the object will be returned during the program's declaration cycle.

Code tracing is possible

getInstance()This method, you will see this singleton mode method in the same file, returning the Container instance .

Next, we will conduct an in-depth analysis of the make method in the container.

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 Preliminary analysis of ThinkPHP container. 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