facade是如何产生的 in laravel

据说Facade是为了简化写法, 好记,也有人说要废除这个特性, 据说这个特性有很好的的testability 测试性, 我不清楚, 这个是怎么说出来的理由是什么?

很多Facade 是系统写好的, 在illuminate\support\Facade下面,

从__callStatic这个方法入手,

public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();

if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}

return $instance->$method(...$args);
}

$instance 就是 这个 facade 的 getFacadeAccessor 返回的字符串在 service container里面所代表的的对象,
这里面有个 throw 提示错误, 貌似这样比较的友好么?

return $instance调用facade静态调用的方法. 完了。 那对测试有何影响?

猜你喜欢

转载自www.cnblogs.com/qinqiu/p/12980853.html
今日推荐