I recently read laravel4 code and found that the set and get methods in the Config class (Repository) are not static methods, but you can use Config: get (& #039; app. url & #039;), Config: set (& #039; app. url & #039;, & #039; xxx. xx & #039;) How is this implemented?
I recently read laravel4 code and found that the set and get methods in the Config class (Repository) are not static methods, but you can use Config: get ('app. url '), Config: set ('app. url ', 'HTTP: // xxx. xx ')
How is this implemented?
Reply content:
I recently read laravel4 code and found that the set and get methods in the Config class (Repository) are not static methods, but you can use Config: get ('app. url '), Config: set ('app. url ', 'HTTP: // xxx. xx ')
How is this implemented?
See the following code in sequence.
Step 0
Https://github.com/laravel/laravel/blob/master/app/config/app.php#L144
'aliases' => array( 'App' => 'Illuminate\Support\Facades\App', 'Artisan' => 'Illuminate\Support\Facades\Artisan', 'Auth' => 'Illuminate\Support\Facades\Auth', 'Blade' => 'Illuminate\Support\Facades\Blade', 'Cache' => 'Illuminate\Support\Facades\Cache', 'ClassLoader' => 'Illuminate\Support\ClassLoader', 'Config' => 'Illuminate\Support\Facades\Config',);
Step 1
Https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Facades/Config.php
Step 2Https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Facades/Facade.php#L198
public static function __callStatic($method, $args){ $instance = static::resolveFacadeInstance(static::getFacadeAccessor()); switch (count($args)) { case 0: return $instance->$method(); case 1: return $instance->$method($args[0]); case 2: return $instance->$method($args[0], $args[1]); case 3: return $instance->$method($args[0], $args[1], $args[2]); case 4: return $instance->$method($args[0], $args[1], $args[2], $args[3]); default: return call_user_func_array(array($instance, $method), $args); }}
The key is__callStatic
Method inheritance. When an undefined static method is executed, if the class method defines this__callStatic
The program will execute the code in this place.
Config
Class is actuallyIlluminate\Support\Facades\Config
Alias,
WhenConfig::set()
AndConfig::get()
When the static method is usedStep 2
.$instance
YesRepository
.
Interceptor Used__callStatic
When a non-existent method is called in static mode, the method is blocked. The first parameter is the static call method name, and the second parameter is an array containing the call method parameters. He processed the interceptor method, for examplecall_user_func
Automatically load the corresponding method.
The source code you post contains a delayed static binding, but it is not important. What is important is this interceptor.
The namespace of PHP is wonderful.