This paper mainly describes the use and implementation of the facade and the depth of the source code analysis.

    preface

    Those of you who use frameworks will know that in 5.1 the framework added a feature called facade, which I will write in this article.

    Anyone who has used this feature knows the benefit, that method calls can be called statically instead of using the static keyword.

    Next, Kaka will take you to explore the story of facade from the following aspects.

    A brief understanding of the benefits of the facade in the framework

    I wrote a configuration file loading article earlier, and at the end of that article I mentioned several ways to get configuration information.

    One of these methods is Config::get(). As we all know in this article, use think\facade\Config must be introduced before using Config to obtain configuration information.

    Although we are using the use think \ facade \ Config, but the actual call method is thinkphp/library/think/facade. The __callStatic method in PHP.

    The createFacade method of the same file is then executed.

    Although I haven’t looked at the source code yet, you should know that the createFacade method is obtained directly from the container class when it is called.

    When learning containers, we all know that the container is using the registration tree mode, which can be directly obtained when the corresponding object instance needs to be used, thus avoiding the creation of a class repeatedly. That’s one of the advantages. Take advantage of the container’s properties

    For the previous use of config, you needed to use the namespace of config and then instantiate it to make the call.

    If config is not available at this point, you need to use the config class you created, and if facade mode is not used, you need to change a lot of code, and it is global.

    But if you use the facade pattern in the framework, you just need to override getFacadeClass, you just need to change the return result inside of it, because where other files are called they don’t care what the instance is called, they just care about the method name and the return result.

    Learn the use of facade in the framework

    Start by creating a controller Facade and write the following.

    This is simply a facade approach to obtaining configuration file information.

    Here you can see the use ofuse ConfigThis is the alias of the config class.

    Alias Settings are set in base.php.

    How to use a facade properly in a framework!

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

    A Sessions class is created here.

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

    This is what happens when you don’t use a facade. You need to import the corresponding class, instantiate it, and then use the instantiated class to make method calls.

    Print the result, and the result is what we expect.

    So how to change this code to facade mode! Follow the kaka step by step.

    Create two directories under the Kaka directory, facade and util

    Why create these two folders?! Util is a class file that can be used in other projects.

    That means we just need to implement one copy and take it in when we use it in another project.

    So you can copy the file directly into the util directory, just remember to change the namespace.

    Then create a new Sessions class under the Facade directory and inherit the facade. Then write the content.

    Now let’s go to the controller and test it out.

    The result is the same as before, but the obvious difference is that after using the facade pattern, it can be directly invoked in a static manner.

    Remember the one good thing about doors?

    Assuming the Sessions utility class stops being used at some point in the future, all we need to do is change the contents of the getFacadeClass method.

    Optimize the use of facades in the framework

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

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

    Next kaka will provide you with a feasible solution, if you have other solutions can put forward ha! See you in the comments section.

    There are not just one or a few custom facade classes in a normal development effort, but many in a complex project.

    Since there are many, it needs to be managed.

    Start by creating a configuration class that belongs to the facade.

    Map the proxy class to the actual class, and then set the alias.

    In this case, you need to create a hook file to implement both the facade class registration and the facade class name registration.

    The final step is that the hook file is created but not executed.

    So when should the hook file be executed! That is to load the application when it is initialized.

    The initialization configuration applied in TP5.1 is in the application/tags.php file.

    Add the hook file to the application initialization configuration.

    test

    The final step is to test, is still executing application/index/controller/Facade. The PHP file getUserInfo method.

    According to the test results, we can know that there is no problem in the coding of our scheme.

    There is a problem that the alias of the facade class is defined in the hook, but is not used here.

    Let’s test this with an alias.

    Four, facade class source code analysis

    Before parsing the source code, recognize two methods.

    • __callStatic: this method is called when accessing static methods that do not exist.
    • call_user_func_array: You can use this function directly to call functions.

    We’ll start parsing by getting the configuration file

    performConfig::get('facade.');The file will be executedthinkphp/library/think/facade/Config.phpIn the.

    In this file, as mentioned earlier, the getFacadeClass method returns the corresponding alias if it exists.

    If not, use the bind method for facade binding.

    If you don’t understand here, you need to go to the documentation and have a good look at the section on facade!

    The get method does not exist in the above class, so it is called directlythinkphp/library/think/Facade.phpIn the file__callStaticMethods.

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

    This class is then executedcreateFacadeThis method

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

    Because you have the same method in a subclass, you have the same method in this class but the method in this class doesn’t return any value.

    Do you have a little bit of confusion at this point as to where static actually executes methods? Or think of it this way, why do we do subclass methods.

    Keep these questions will be in the following to tell you carefully, first to finish reading the source code of the facade class.

    And in this method I’m going to focus on a couple of places that I’ve circled.

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

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

    The third is the container mentioned in the previous article. I will not elaborate on it here. If not, click on the home page to see the previous article.

    CreateFacade method

    Static keyword

    I have to mention the static keyword here.

    New learning partners will probably only know that static is used to define static variables and static methods.

    I’m not going to show 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 facade source code, according to the facade source code adaptation.

    There are two new files called test and test1.

    Test inherits from the test1 file and has the same method getKaka.

    Create two new files

    The source code of the test

    The test source

    Test1 source

    The controller makes a call

    Print the result Is there any confusion at this time, how is it printed here147Rather than456!

    Modify test1 code to change static to self

    Print the result

    Static self = static self = static self = static self

    This is where the documentation comes in, but when you open up the PHP documentation, you’ll notice that the static section doesn’t address this type of situation.

    After many tests and access to information, the final summary results are as follows.

    Static ::$test calls subclasses by default if they are inherited, otherwise they call themselves

    Self ::$test is called by default if it is inherited

    In this example, when test inherits from test1.

    When static calls getKaka from test1, the default call is getKaka, the subclass method of the test class.

    When self calls getKaka from test1, the default call is getKaka from class test1, which is the method of this class.

    This small detail is also discovered by Kaka accidentally, if there is anything wrong can be put forward, Kaka make corrections.

    Because there is another case for inheritance, which kakha will test privately, it will not be explained here.

    Here to explain the static is aimed to explain thinkphp/library/think/Facade. The PHP file in this line of code.

    Since this line of code calls methods that exist in both subclasses and superclasses, I wrote a brief introduction so you wouldn’t get confused.

    thinkphp/library/think/Facade.php

    Six, summarized

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

    The source code of facade class is very simple, in addition to a few less common knowledge points, the code is believed to see clearly.

    Here is mainly after reading the facade class, do a small summary.

    Facade class is mainly combined with a container to achieve a function, because the need for the container to return the corresponding instance, the article on the container has been completed, if there is no container can be at the beginning of the article to see the corresponding article.

    This article gives you an introduction to the use of facade in containers, and provides you with the best way to use it. The best way here is my personal opinion, because this way has been used for nearly two years.

    Both robustness and extensibility of the code are very practical.

    When a class inherits from a class, the default method of the subclass is called when the parent class uses the static keyword.

    The summary here is only for the examples in this article.

    Return call_user_func_array([static::createFacade(), $method], $params);

    Because in the previous use of the argument is directly method, but here we encounter the array form, so the array of two values represent what?

    The first value is the instance, and the second value is the method in the instance.

    The call_user_func_array method will not be used as an example, except that it will execute the method passed in.

    Here on the facade of the source code analysis is over, the most important thing is to understand the container, because the facade is on the basis of the container to achieve, this is the first container to write the reason for the facade.

    There is also about the use of facade kakaka also gave a plan, if you have a better plan can give a general idea in the comment section.

    Adhering to learning, blogging and sharing is the belief that Kakha has been upholding since she started her career. I hope the articles on the Internet can bring you a little help. I’m Kaka. See you next time.