Home>Article>PHP Framework> ThinkPHP facade source code analysis
“This article mainly describes the use and implementation process of the facade, and analyzes the source code.
”
Partners who use the framework should know that in 5.1, the framework added a new feature, which is the facade that this article will write, that is facade feature.
Anyone who has used this feature understands the benefit, that is, the method call can be directly called statically, without using the keyword static to define it.
Next, Kaka will take you to explore the story of facade from the following aspects.
I have written about configuration file loading before One article, at the end of that article mentioned several ways to obtain configuration information.
One of the methods is Config::get(). By this article, you should know that when using Config to obtain configuration information, you must first introduceuse think\facade\Config
, and then Because the alias is registered in the system, just useuse Config
.
Although we are usinguse think\facade\Config
, the actual method called is__callStatic# in
thinkphp/library/think/Facade.php##method.
createFacademethod of the same file will be executed.
createFacademethod, it is obtained directly from the container class.
getFacadeClassmethod. You only need to change the return result and define it yourself. Because they don't care what the instance calls when other files call it, they only care about the method name and return result.
Here you can see thatuse Config
is used, which is the alias of the config class.
The alias setting is set inbase.php
.
How to use facade correctly in the framework!
Create a new folder facade in the app directory to specifically store facade classes.
A Sessions class is created here.
First do a test to check whether there is any problem with the code. Test in the controller's facade file.
This is how it is handled when the facade is not used. You need to introduce the corresponding class, then instantiate it, and then use the instantiated class to make method calls.
Print the result, the result is what we expected.
So how do you change this code to facade mode! Follow Kaka's footsteps step by step.
First create two directories in the kaka directory, namely facade and util
Why should we create these two folders! Everyone should know that util is a tool class, and this class file can be shared in other projects.
That is to say, we only need to implement one copy and then use it directly in other projects.
So you can copy the file directly to the util directory, just remember to modify the namespace.
Then create a new Sessions class in the facade directory and inherit Facade. Then write the content.
At this point we are coming to the controller to test it.
You will find that the result is the same as before, but the obvious difference is that after using the facade mode, you can directly use the static method to call.
Do you still remember one of the benefits of facade mentioned before?
Assuming that this Sessions tool class will stop being used in the future, then we only need to modify the content in thegetFacadeClass
method.
Although the desired effect is displayed, the code is still not concise and beautiful enough, and the structure is relatively confusing.
Next, Kaka will provide you with a feasible plan. If you have other plans, you can put them forward! See you in the comments section.
In normal development work, it is impossible to have only one or a few customized facade classes. In complex projects, there will be many facade classes.
Since there are many, they need to be managed.
First create a configuration class belonging to the facade.Map the proxy class with the actual class, and then set the alias.
At this time, you need to create a hook file, and put the facade class registration and facade category name registration in it for execution.
The last step is that the hook file is created but not executed.
So when should the hook file be executed? That is to load it when the application is initialized.
The application initialization configuration in TP5.1 is in the fileapplication/tags.php
.
Just configure the hook file in the application initialization configuration item.
Testing
The last step is to test, still execute the # in theapplication/index/controller/Facade.php
file ##getUserInfoMethod.
Have you found a problem here, that is, since the alias of the facade class is defined in the hook, it is not used here.
Next we use aliases to test it.Before parsing the source code Know two methods.
__callStatic
: This method is called when a non-existing static method is accessed.
call_user_func_array
: You can use this function to call the function directly.
We will start parsing from obtaining the configuration file
ExecutionConfig::get('facade.');
will be executed In the filethinkphp/library/think/facade/Config.php
.
In this file, as mentioned before, if thegetFacadeClass
method exists, it will directly return the corresponding alias.
If it does not exist, you need to use the bind method to bind the facade.
If you don’t understand here, you need to go to the documentation and take a good look at the facade chapter!
There is no get method in the above class, so the__callStatic
method in thethinkphp/library/think/Facade.php
file will be called directly.
This method is directly explained at the beginning of the article. This method will be called when accessing a non-existent static method.
Then thecreateFacade
method in this class will be executed
There is a line of code in this method that looks like this$facadeClass = static ::getFacadeClass();
This code will be explained in detail below.
Because there are the same methods in subclasses and the same methods in this class, but the methods in this class do not have any return value.
At this time, are you confused at all? Which method will be executed by the static used here? Or think about it this way, why the subclass method is executed.
Keep these questions and I will explain them to you in detail below. Let’s read the source code of the facade class first.
In this method, we mainly look at the several places I circled.
The first place is to get the alias of the class from thegetFacadeClass
method of the subclass.
The second point is that when the subclass does not have agetFacadeClass
method, it is obtained from the manually bound properties.
The third place is the container mentioned in the previous article. I won’t explain it in detail here. If you don’t know how, click on the homepage to read the previous article.
in I have to explain the static keyword here.
New learning partners may only know that static is used to define static variables and static methods.
Of course, I won’t tell you how to define static methods and static variables here, but I will talk about a very, very small detail.
Let’s look at an example first. This example was also adapted by Kaka based on the facade source code when reading the facade source code.
Kaka has created two new files here, namely test and test1.
test inherits the test1 file and has the same method getKaka.
test source code
test1 source code
Controller calls
Print resultsAre you a little confused at this time? Why is147
printed here instead of456
!
Modify the code of test1 and change static to self
Print the results
I believe everyone can read the code using self I understand, so why do I get results that may not be clear when using static!
At this point, the document starts to take effect, but when you open the PHP document, you will find that there is no explanation of this situation in the static article.
After many tests and information review by Kaka, the final summary results are as follows.
static::$test If it is inherited, it will call the subclass by default, otherwise it will call itself
self::$test If it is inherited, it will call this class by default
To illustrate in this example, when test inherits test1.
When using static to call methodgetKaka
in test1, the default call isgetKaka
in the test class, which is the method of the subclass.
When using self to call the methodgetKaka
in test1, the default call isgetKaka
in the test1 class, which is the method of this class.
This small detail was also discovered by Kaka accidentally. If there is anything wrong, you can point it out and Kaka will make changes.
Because there is another situation in inheritance, Kaka will test it privately, so I won’t explain it here.
The explanation of this static here is mainly to explain this line of code in thethinkphp/library/think/Facade.php
file.
Because the method called by this line of code exists in both the subclass and the parent class, Kaka wrote it to give a brief introduction in order to avoid confusing everyone.
The source code of the facade class is very simple. Except for a few uncommon knowledge points, I believe the code can be understood clearly.
This is mainly to make a short summary after reading the facade class. The facade class is mainly a function implemented in combination with the container, because the container is needed to return the corresponding instance. The article about the container has also been completed. If you have any questions about the container, you can go to the beginning of the article. Just read the corresponding article. This article introduces how to use the facade in the container, and provides you with the best way to use it. The best here is Kaka’s personal opinion, because in this way Kaka uses nearly two years.It is very practical in terms of code robustness and scalability.
Then about the static keyword, I would like to add some unpopular knowledge to everyone. When a class inherits a class, when the parent class uses the static keyword, the method of the subclass is called by default.
The summary here is only for the examples in this article.
In fact, Kaka wants to explain one point to you here, which isreturn call_user_func_array([static::createFacade(), $method], $params);
Because in the previous usage, the parameters were directly methods, but here we encounter the array form, so what do the two values in this array represent?
The first value is the instance, and the second value is the method in the instance.
Aboutcall_user_func_array
The use of this method will not be done by KaKa. You only need to know that it will execute the incoming method.
The source code analysis of the facade ends here. The most important thing is to understand the container, because the facade is implemented on the basis of the container. This is why Kaka writes the container first and then writes the facade.
In addition, Kaka also gave a plan for using the facade. If you have a better plan, you can give a general idea in the comment area.
“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 facade source code analysis. For more information, please follow other related articles on the PHP Chinese website!