In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample Analysis of ThinkPHP Container", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample Analysis of ThinkPHP Container".
1. Singleton model
There are two design patterns that you must understand before learning about containers and facades, singleton pattern and registration tree pattern.
First, give a simple explanation of the singleton pattern.
Has a constructor and the property private has a static member variable to hold an instance of the class and a static method to access the instance
The following is a simple singleton pattern implemented, comparing the above three features to see if they are consistent.
The static variable is instance
Has a structure and is private.
The last one is that there is a static method called getInstance.
Let's do a simple test.
It was also tested in the index controller and was called four times to verify that its class was instantiated only once.
Visit this method to take a look.
New-class is executed only once, which directly proves that the created class is instantiated only once. A question before clicking here is why the constructor here uses private properties.
Have you ever had this question before? Kaka will take you to answer it.
The constructor that defines private properties in this class is to prevent its class from being instantiated externally.
When this class is instantiated externally, the error in the following figure is reported.
So why mention the singleton mode here? Because it will be used in the source code of the next learning container
For example, in the following figure, there is an instance in the thinkphp/library/think/Container.php class that gets the current container.
So far, the singleton pattern is simply understood, and the understanding of the singleton pattern is also for a better understanding of the container.
Second, registration tree mode
Why do you say this registration tree pattern here? because registering the tree pattern in the framework is a dominant position, you must understand it!
Then what is a registered tree model?
The registration tree mode is to register an object instance with a tree (the tree here is not a real tree! Is to register in a global property) and then you can get the corresponding object instance from the global tree through internal methods.
If you say this, you certainly can't understand it better. Next, Kaka will take you to take a look at a simple case to have a simple understanding.
All you need for a registration tree mode is four, the pool of the registration tree, mount objects to the registration pool, get objects from the registration pool, and unload objects from the registration pool.
The figure below is a simple registration tree pattern written by Kaka.
If the code does not understand, it needs to make up the foundation, ha!
Next, create a TestTree file in the same directory
Come to the controller to test whether there is a problem with the registration tree mode written.
Be sure to pay attention to the problem of namespaces when doing tests! The kaka directory here was previously configured in the autoload of the class. If you can't, you can check it out in the first issue of the article.
This is equivalent to instantiating the class TestTree first.
Then use the registration tree mode to register this instance with the object tree pool
Finally, using the get method to get this class out, you can directly call the method in TestTree.
Finally, take a look at the final print result, which is the return value of the getTreeContent method in the TestTree class.
Registration tree mode is the above click to explain these contents, is not for the source code to learn, these are the contents we have to learn to use.
Third, how to understand control inversion and dependency injection
In fact, these two refer to a thing, is just a programming idea, do not think so difficult to understand and high-end.
So what is a container? the container faces and understands that it is something to hold things. In programming, our common variables and object properties are all containers. What can be contained in a container depends entirely on the definition of the container.
However, now we are talking about another kind of container, which stores not text and numerical values, but objects, classes and interfaces through this container to achieve many advanced functions, the most commonly used is code decoupling and dependency injection.
So why are there two concepts, control inversion and dependency injection? As mentioned above, they actually refer to one thing, but from a different point of view.
Just like you are your father's son, you are still your grandfather's grandson, both son and grandson refer to the same person. Just look at the problem from a different point of view.
Control reversal
Is to look at the problem from the perspective of the container, the container controls the application, and the container injects the external resources needed by the application in the reverse direction.
Dependency injection
Is to look at the problem from the perspective of the application, which relies on the container to create and inject the external resources it needs.
Action
It is mainly used to reduce the degree of coupling between code.
Effectively separate the external resources needed by the object and the application.
The following two pictures can clearly illustrate the problem.
Let me give you a whole simple case.
Define two classes as Person and Car, which are instantiated in Person and call the pay method in Car.
Then call it in the controller, and the print result must be the 123 returned by Car, so this will not be printed.
Insert a picture description here
At this time, we modify the code, pass the Car class directly to the Person class, and call the corresponding method directly with the passed object in the Person class.
This is just a simple implementation process, in order to lay the groundwork for reading the framework container code, the container injection in the framework will be described in detail later.
4. The mechanism of inevitable reflection
I don't know if you have understood the reflection mechanism of GO, but Kaka was a little dizzy after seeing the reflection mechanism of go at that time.
But after seeing the reflection of PHP later, I not only have some in-depth understanding of the reflection of go, but also have a better understanding of the reflection of PHP.
The concept of reflection was introduced in PHP5.0, and what Kaka knows in the current framework is that both thinkphp and laravel use reflection to implement dependency injection.
The understanding of reflection: it is actually getting something other than the root from the root. In programming, it means that as long as you know a class, you can know all the properties and methods of that class.
Case
This is just a simple implementation case that gets all the methods and properties of the class. You can see if the print result in the following figure is consistent with TestReflection.
This also shows a problem from the side, that is, it will expose some information that should not have been exposed.
There are many more interfaces for reflection, here are a few commonly used, and the rest are parsed in the framework source code.
Use reflection to execute the methods of a class
The printed result is Kaka.
Use reflection to execute a method with parameters in a class
Insert a picture description here
Use reflection to execute a method without parameters in a class
You can try other methods yourself, because this reflective interface is not used in basic development at ordinary times, and what this Kaka introduces to you is that you can use it after reading the source code.
Now that you know about reflection, what can reflection do? There is a function point that automatically generates documents.
If you want to know more about the use of the interface, you can go to the official view of the corresponding interface information.
Insert a picture description here
After understanding the reflection, let's get down to business, and we need to officially get into our container session. Only when the above foundation is laid well can the next container be better understood.
5. Play with your own container class
After the difficulty of 9981, we finally came to the container. In this link, let's first implement a container of our own, concatenating the singleton pattern, registration tree mode, and reflection explained earlier, so as to deepen our impression and better understanding.
I still remember that I talked about a method called dependency in dependency injection, which is to decouple the code by doing dependency injection.
But this time! Containers will be used to solve this problem.
First of all, define the required class, this class uses the singleton pattern and registration tree pattern, the previous article did not have a good look, be sure to read carefully, otherwise the following will be very difficult to understand.
Insert a picture description here
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.