门面(Facade) 门面为容器中的(动态)类提供了一个静态调用接口,它的功能就是可以让类无需实例化而直接进行静态方式调用。
1, 新建 \common\
2, 新建 \facad\
3, 新建 \common\Test.php
4, 新建 \facad\Test.php
5, 新建 \controller\Facaddemo.php
1, 新建 \common\
2, 新建 \facad\
3, 新建 \common\Test.php
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2023/3/23 * Time: 20:20 */ namespace app\common; class Test { public function index($name){ return '这是这个值'.$name; } }
4, 新建 \facad\Test.php
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2023/3/23 * Time: 20:24 */ namespace app\facad; use think\Facade; class Test extends Facade { protected static function getFacadeClass(){ return 'app\common\Test'; } }
5, 新建 \controller\Facaddemo.php
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2023/3/23 * Time: 20:30 */ namespace app\controller; use app\facad\Test; class Facadedemo { public function index(){ //2种写法。1,直接写。 2,引入写。 //return \app\facad\Test::index('$这是传进的参数'); return Test::index('$这是传进的参数'); } } }
请求: http://www.tp.com/public/index.php/Facadedemo 结果: 这是这个值$这是传进的参数