Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ThinkPHP facade

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of ThinkPHP facade for everyone, Xiaobian thinks it is quite practical, so share it for everyone to make a reference, I hope you can gain something after reading this article.

I. A brief understanding of the benefits of facades in the framework

There was a previous article about loading configuration files, and several ways to obtain configuration information were mentioned at the end of that article.

One of the ways is Config::get(), to this article should know that when using Config to obtain configuration information, you must first introduce use think\facade\Config, and because the alias is registered in the system, so use Config directly.

Although we used use think\facade\Config, the actual method called is the__callStatic method in thinkphp/library/think/facade.php.

The createFacade method of the same file is then executed.

Although I haven't seen the source code yet, it's good to know that when calling the createFacade method, it is directly obtained from the container class.

When learning the container, we all know that the container uses the registration tree pattern. When you need to use the corresponding object instance, you can get it directly, so as to avoid the repeated creation of a class. This is one of the advantages. Using the characteristics of the container

For the previous use of config, you need to use config's namespace and instantiate it before calling it.

If config is not used at this time, you need to use the config class you created. If you do not use the facade pattern, you need to modify a lot of code, and it is global.

However, if you use the facade pattern in the framework, you only need to rewrite the getFacadeClass method, you only need to change the return result inside, because for other file calls, they don't care what the instance calls, just care about the method name and the return result.

II. The use of facades in learning frameworks

First create a controller facade and write the following.

Here is a simple way to get profile information using facade.

You can see here that use Config is used, which is an alias for the config class.

The alias settings are set in base.php.

How to use facade correctly in framework!

Create a new folder facade under the app directory to store facade classes.

Here we create a Session class.

Do a test first to see if the code is written incorrectly. Test in the facade file of the controller.

This is how it is handled when no facade is used. You need to introduce the corresponding class, then instantiate it, and then use the instantiated class to make method calls.

Print the results, and the results are what we expect.

So how does this code change to facade mode? Step by step with the clicking footsteps.

First, create two directories under kaka directory, namely facade and util

Why create these two folders! util As you all know, this is the utility class, which is a class file that can be shared among other projects.

That is to say, we only need to implement one copy and then directly take it when we use it in other projects.

So you can copy the file directly to the util directory, remember to modify the namespace.

Then create a new Sessions class in the To Facade directory and inherit Facade. Then write something down.

At this point we come to the controller to test it.

You will find that the result is the same as before, but one obvious difference is that after using facade mode, you can call it directly in static mode.

Remember one of the benefits of facade mentioned earlier?

Assuming that the Sessions utility class is discontinued one day, we only need to modify the contents of the getFacadeClass method.

III. Optimize the use of facades in the framework

In the previous section we went from instantiating classes to implementing the same functionality using facade.

Although the desired effect is shown, the code is still not concise and elegant, and the structure is confusing.

Next, Kaka will provide you with a feasible plan. If you have other plans, you can come up with it! See comments section.

In normal development work, custom facade classes cannot be one or several, but in complex projects there will be many.

Since there are many, it needs to be managed.

First create a configuration class that belongs to Facades.

And map the proxy class to the actual class, and then set the alias.

At this point, you need to create a hook file that will store both the facade class registration and the facade class name registration.

There is one final step, where the hook file is created but not executed.

When should the hook file be executed? That is to load when the application is initialized.

The configuration that applies initialization in TP 5.1 is in the file application/tags.php.

The hook file can be configured in the configuration item of application initialization.

test

The final step is to test, again by executing the getUserInfo method in the application/index/controller/Facade.php file.

According to the test results, we can know that there is no problem in writing our solution code.

Did you find a problem here, that is, since the alias of the facade class is defined in the hook, it is not used here.

Let's test it with aliases.

Fourth, the facade class source code analysis

Before parsing the source code, recognize two methods.

__callStatic: This method is called when a static method that does not exist is accessed.

call_user_func_array: This function can be used directly to call functions directly.

So we're going to start by getting the profile.

Execute Config::get ('facade. '); will be executed into the file thinkphp/library/think/facade/Config.php.

In this file, as mentioned earlier, if there is a getFacadeClass method, it will directly return the corresponding alias.

If it doesn't exist, you need to use the bind method to bind the facade.

If you don't understand here, you need to go to the document and take a good look at the facade that chapter ha!

There is no get method in the above class, so the__callStatic method in the thinkphp/library/think/Facade.php file is called directly.

This method is the one described directly at the beginning of the article and is called when accessing static methods that do not exist.

Then the createFacade method in this class is executed

Inside this method is a line of code that looks like this: $facadeClass = static::getFacadeClass(); this is explained in more detail below.

Because there are the same methods in the subclass, there are the same methods in this class, but the methods in this class do not return any value.

Do you have any doubts about where static is going to execute? Or think about it, why would you execute a subclass method?

Keep these questions will be given to you in detail below to talk about, first to the facade class source code read.

In this method, mainly look at a few places that I have circled.

The first is to get the alias of the class from the getFacadeClass method of the subclass.

The second is from manually bound properties when the subclass does not have a getFacadeClass method.

The third place is the container mentioned in the previous article. I won't explain it in detail here. If I don't, click on the homepage to see the previous article.

V. Static keywords

I have to explain the static keyword here.

The newly learned partner estimator only knows 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, but a very, very small detail.

First look at an example, this example is also in reading the facade source code, kaka adapted from the facade source code.

Click, click, click. Two new files have been created here: test and test1.

test inherits the test1 file and has the same getKaka method.

Test source code

test1 source code

controller calls.

Is there any doubt about the printing result at this time? How can it print out 147 instead of 456?

Change test1's code from static to self

print result

The code that uses self believes that everyone can see clearly, so why use static to appear may not understand the result!

This is when the documentation starts to work, but when you open the PHP documentation you will find that this is not explained in static.

After many times of testing and consulting data, the final summary results are as follows.

static::$test If there is an inheritance, the default calls the subclass, otherwise it calls itself

self::$test This class is called by default if inherited

In this example, it is shown that when test inherits test1.

When static calls getKaka in test1, the default call is getKaka in the test class, i.e., the subclass method.

When using self in test1 to call the method getKaka, the default call is getKaka in test1 class, which is the method of this class.

This small detail was also discovered accidentally by Ka Ka. If there was anything wrong, he could bring it up and make changes.

Because there is another situation in inheritance, Ka Ka will conduct tests privately, which will not be explained here.

The explanation of static here is mainly to explain this line of code in the thinkphp/library/think/Facade.php file.

Because this line of code calls methods that exist in both child and parent classes, Kaka wrote a simple introduction in order not to confuse everyone.

VI. SUMMARY

First come to a facade flow chart, you can see more clearly the specific implementation process of the facade class.

About "ThinkPHP facade sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report