Laravel-php: How does one implement static calls using non-static methods?

Source: Internet
Author: User
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 2

Https://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__callStaticMethod inheritance. When an undefined static method is executed, if the class method defines this__callStaticThe program will execute the code in this place.

ConfigClass is actuallyIlluminate\Support\Facades\ConfigAlias,

WhenConfig::set()AndConfig::get()When the static method is usedStep 2.$instanceYesRepository.

Interceptor Used__callStaticWhen 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_funcAutomatically 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.

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.