Facade is actually a container class static proxy, he can let you in a static way to call any object stored in the container any method, the following this article mainly introduces about the laravel in the facade loading process and the principle of relevant information, the need for friends can refer to.
Objective
This article mainly introduces about the laravel in the facade loading process and the principle of related content, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction bar.
Brief introduction
Facades (pronunciation:/fəˈsäd/) provides a "static" interface for the classes available in the application's service container. You do not have to use a large string of namespaces, or instantiate objects, to access the specific methods of the object.
Use Config;class test{Public Function index () {return config::get (' App.name ');}}
Facade start-Up and registration
The boot boot of the facade is registered in the Illuminate\foundation\bootstrap\registerfacades.
Public Function bootstrap (application $app) {facade::clearresolvedinstances (); Facade::setfacadeapplication ($app); Aliasloader::getinstance (Array_merge ($app->make (' config ')->get (' app.aliases ', []), $app->make ( Packagemanifest::class)->aliases ())->register ();}
The default alias configuration is read from aliases under the app profile, and Packagemanifest is the new package Autodiscover rule for Laravel 5.5, where we don't consider the alias provided by the Packagemanifest package for the time being.
Where Array_merge returns an array of the following formats:
"App" = "Illuminate\support\facades\app" "Artisan" + "Illuminate\support\facades\artisan" "Auth" and "=" Illuminate\support\facades\auth "Blade" = "Illuminate\support\facades\blade" ...
The above code will register all the facade into the auto-load via Aliasloader. Its core is PHP's spl_autoload_register.
/** * prepend The Load method to the Auto-loader stack. * * @return void */protected function Register () {if (! $this->registered) { spl_autoload_register ([$this, ' Load ' ], True, true); $this->registered = true; } }
After the registration is complete, all subsequent use classes will complete the automatic loading of the class through the load function.
Note: when defining spl_autoload_register here, the last parameter of the polygon is passed true. When this argument is true, Spl_autoload_register () adds the function to the top of the queue, not the tail of the queue. (This function is preferred to complete the automatic loading)
Other words
<?phpuse config;use app\user;class test{Public Function Index () {config::get (' app.name '); new User ();}}
Whether we use a specific class (App\user) or alias (Config), the load function will be the first to complete the automatic loading, when the function returns False, and then by other auto-load functions to complete the automatic loading (such as composer Psr-4).
In the Aliasloader load method, the alias is automatically loaded using the Class_alias function.
Public function Load ($alias) {if (Isset ($this->aliases[$alias]) {return Class_alias ($this->aliases[$alias], $ alias); }}
About Class_alias Here's an official quote:
class Foo {}class_alias (' foo ', ' Bar '), $a = new Foo, $b = new bar;//The objects is the samevar_dump ($a = = $b, $a = = = $b); Truevar_dump ($a instanceof $b); false//The classes is the Samevar_dump ($a instanceof foo); Truevar_dump ($a instanceof bar); Truevar_dump ($b instanceof foo); Truevar_dump ($b instanceof Bar); True
Loading of the facade
When we are using facade, such as:
<?phpuse config;class test{Public Function Index () {config::get (' app.name ');}}
The Illuminate\support\facades\config class is actually loaded (because we have registered the Class_alias), which is equivalent to:
<?phpuse illuminate\support\facades\config;class test{Public Function Index () { config::get (' App.name ');}}
And all facade inherit from the Illuminate\support\facades\facade class, which defines a __callstatic method in the base class, as we can easily use facade (without the actual listing).
<?phppublic static function __callstatic ($method, $args) {$instance = Static::getfacaderoot (), if (! $instance) { throw new RuntimeException (' A facade root have not been set. '); } return $instance $method (... $args);}
The Getfacaderoot method is used to get the specific real column of the alias class, and we know that all facade classes need to define a getfacadeaccessor method. The possible return values for this method are:
Strings of type string (for example, Config, db)
string-type class strings (such as App\service\someservice)
Object concrete Real-list Object
Closure Closure Package
The Getfacadeaccessor method, such as Config facade, is as follows:
protected static function Getfacadeaccessor () {return ' config ';}
The Getfacaderoot method will getFacadeAccessor()
remove the corresponding real-column object from the container, based on the return value.
public static function Getfacaderoot () {$name = Static::getfacadeaccessor (); if (Is_object ($name)) { return $name;} if (Isset (static:: $resolvedInstance [$name])) { return static::$ resolvedinstance[$name]; return static:: $resolvedInstance [$name] = static:: $app [$name];}
Because the real column of config is already registered in the APP container
<?php//illuminate\foundation\bootstrap/loadconfiguration$app->instance (' config ', $config = new Repository ($ Items));
So the \Config::get('app.name', 'dafault)
actual access is the method of the Repository real column get('app.name', 'default')
.
Summarize