Read Laravel and see how PHP implements facade?

Source: Internet
Author: User

Just beginning to learn Laravel will come into contact with routing

123 Route::get(‘/‘function() {    returnview(‘welcome‘);});

Later I seriously read the code of the Route class, surprised to find that there is no get this method, then learned that Laravel used the facade mode.

Facade is essentially a class that "pushes work to others."

The value of facade existence can be discussed from the service container. Service container, see my other blog post, address: http://www.cnblogs.com/sweng/p/6430374.html

For example, I do not know if you have ever written code before Obj->method (ARG1,ARG2)->func (ARG3,ARG4), the experience. The reader of the service container knows that this line of code is to take out the object in the service container and invoke his method. This code is acceptable to developers who are familiar with which classes are registered in the service container. But it's too much of an elegant way to write a lengthy form like a route definition. So using facade mode can be a good way to simplify the code length.

Let's write a DB class first.

123456789101112 namespaceAPI;classDB{    publicfunction__construct($args){    }    publicfunctionWrite($str){        echo‘Write:‘.$str.PHP_EOL;    }    publicfunctionRead($str){        echo‘Read:‘.$str.PHP_EOL;    }}

Database read and write is a very common operation of the whole system. But DB class is registered in the service container, every time the database read and write to the DB Class object from the service container, it is very inconvenient.

We write a facade class

123456789101112131415161718 classFacade{    publicfunction__construct(){        //    }        publicstaticfunctiongetInstance($classname,$args){        returnnew$classname($args);    }        publicstaticfunctiongetFacadeAccessor(){        //    }        publicstaticfunction__callstatic($method,$arg){        $instance=static::getInstance(static::getFacadeAccessor(),[1,2,3]);        returncall_user_func_array(array($instance,$method),$arg);    }}

To understand this class, we just focus on the last function, the __callstatic magic method. This method is called the __callstatic method, which is the role of a "candidate" when the facade type object calls a function that he has not defined himself.

We'll define a Dbfacade class.

12345 classDBFacade extends Facade{    public static function getFacadeAccessor(){        returnAPI\DB::class;    }}

Each facade subclass implements the Getfacadeaccessor method, which returns just a string of class names used to substitute the GetInstance method to create a truly "do-it-yourself" class.

At this point, facade is ready for use, and we call the static method of Dbfacade

1 DBFacade::Write(‘hello‘);

Reading the code, we found that, in fact, Dbfacade is not the Write method, and then call his father class facade __callstatic Magic method, Magic method we have in the parent class has been implemented.

Previously heard peer complaints, PHP grammar chaos, difficult to remember. But in fact, like the magic method of facade implementation of the very concise, visible syntax design subtle.

Read Laravel and see how PHP implements facade?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.