学习笔记: laravel 的Facade,类静态方法使用原理

class B
{
    public $arrs = array();

    public static function __callStatic($method, $params)
    {
        $class = get_class() . '_release';
        $instance = new $class;
        return call_user_func_array(array($instance, $method), $params);
    }
}

class B_release
{
    public $arrs = array();

    public function set($name, $value){
        $this->arrs[$name] = $value;
        return $this;
    }

    public function get(){
        return $this->arrs;
    }
}


$r = B::set('a', 111)
    ->set('b', 111)
    ->set('c', 111)
    ->set('d', 111)
    ->set('e', 111);

print_r($r->get());

$rr = B::set('a', 111)
    ->set('e', 222);

print_r($rr->get());

// 返回结果
Array
(
    [a] => 111
    [b] => 111
    [c] => 111
    [d] => 111
    [e] => 111
)
Array
(
    [a] => 111
    [e] => 222
)

 

赞 (6)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址