官方说明:门面(Facade)门面为容器中的类提供了一个静态调用接口,相比于传统的静态方法调用, 带...
官方说明:
门面(Facade)
门面为容器中的类提供了一个静态调用接口,相比于传统的静态方法调用, 带来了更好的可测试性和扩展性,你可以为任何的非静态类库定义一个facade类。

代码:
// 静态代理
namespace app\common;
class Test{
public function Test1($name){
return 'hello'.$name;
}
}
// 传统访问:
// $test = new Test;
// $test->Test1('zhuzhuxia');
// 使用静态方式调用:
// 创建一个静态继承
// 建议与动态类命相同
namespace app\facade;
class Test extends \think\Facade{
protected static function getFacadeClass(){
return 'app\common\Test';
}
}
// app\facade\Test 代理了 app\common\Test
// 随即即可使用静态方法调用,当然,必须先进行use
// use app\facade\Test;
// Test::Test1('111');
// 或者直接使用命名空间
// \app\facade\Test::Test1('111');
namespace app\index\controller;
use app\facade\Test;
class Demo{
public function Demo1($name='demo'){
return Test::Test1($name);
return \app\facade\Test::Test1($name);
}
}然而,如果没有在静态代理类中显示指定要绑定的类名,则需要修改代码
namespace app\facade;
class Test extends \think\Facade{
// protected static function getFacadeClass(){
// return 'app\common\Test';
// }
}
// 就需要用动态显示绑定方法绑定一下
namespace app\index\controller;
use app\facade\Test;
class Demo{
public function Demo1($name='demo'){
\think\Facade::bind('\app\facade\Test','\app\common\Test');
return Test::Test1($name);
return \app\facade\Test::Test1($name);
}
}系统内置类


漠漠睡