facade模式就是为动态方法创建一个静态代理,从而实现静态调用
假如有一个动态方法
app\common\test
<?php
namespace app\common;
class test
{
public function hello($name)
{
echo 'Hello'.$name;
}
}
为他创建一个静态代理facade
app\facade\Test
<?php
namespace app\facade;
use think\Facade;
class Test extends Facade
{
protected static function getFacadeClass()
{
return 'app\common\test';
}
}
调用
Test::hello('222name');
如果没有通过getFacadeClass指定动态方法,可以在调用时动态绑定,也可以在应用公共文件中全局绑定
\think\Facade::bind([
'app\facade\Test' => 'app\common\test',
'app\facade\Info' => 'app\common\info'
]);