素材牛VIP会员
php非静态方法如何实现静态调用?
 踏***e  分类:PHP代码  人气:942  回帖:4  发布于6年前 收藏

最近看laravel4的代码,发现其中Config类(Repository)中的set和get方法并不是静态方法,但调用时却可以使用Config::get('app.url'), Config::set('app.url', 'http://xxx.xx')等

请问这个是如何实现的?

 标签:phplaravel

讨论这个帖子(4)垃圾回帖将一律封号处理……

Lv2 入门
yz***zw 学生 6年前#1

使用了拦截器__callStatic,当静态方式调用一个不存在的方法,会被这个方法拦截,第一个参数是静态调用方法名,第二个参数是一个包含调用方法参数的数组。他在拦截器方法里面做了处理,比如使用了call_user_func去自动加载对应的方法。

看你贴的源码里面还有句延迟静态绑定,不过不重要,重要的就是这个拦截器。

Lv1 新人
km***04 JS工程师 6年前#2

请看依次下面代码。

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

<?php namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Config\Repository
 */
class Config extends Facade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'config'; }

}

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);
    }
}

关键就是 __callStatic 方法的继承。在执行未定义的静态方法时,如果类方法中定义了这个 __callStatic,程序就会执行这个地方的代码。

Config 类实际上是 Illuminate\Support\Facades\Config 的别名,

当调用 Config::set()Config::get() 静态方法时,就会执行 Step 2 中的代码。$instance 就是 Repository的一个实例。

Lv7 码师
ha***23 职业无 6年前#3

写了一个简单的例子实现, 当然laravel比这个要复杂点,但是原理是一样的


class Config
{

    private $self;

    private $config = [];

    private function getInstance()
    {
        if ($this->self === null) {
            $this->self = new Config();
        }

        return $this->self;
    }

    protected function set($key, $value)
    {
        $this->config[$key] = $value;

        return $this;
    }

    protected function get($key)
    {
        return $this->config[$key];
    }

    public function __callStatic($method, $arguments)
    {
        $instance = static::getInstance();

        if (method_exists($instance, $method)) {
            return call_user_func_array([$instance, $method], $arguments);
        }
        
        throw new BadMethodCallException();
    }
}
Lv6 码匠
黑***心 移动开发工程师 6年前#4

PHP的namespace就是奇葩

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取