用四种方法实现请求对象的调用
1. new
<?php
namespace app\index\controller;
use think\Request;
//1.实例化类获取信息
class Demo1
{
public function test()
{
dump((new Request())->param()) ;
}
}2. facade
<?php
namespace app\index\controller;
use think\Facade\Request;
//2.通过静态代理的方式访问获取信息
class Demo2
{
public function test()
{
dump(Request::param()) ;
}
}3. Requst $request
<?php
namespace app\index\controller;
//3.通过依赖注入的方式访问获取信息
class Demo3
{
public function test(\think\Request $test)
{
dump($test->param()) ;
}
}4. $this->request
<?php
namespace app\index\controller;
use think\Controller;
//通过继承父类获取信息
class Demo4 extends Controller
{
public function test()
{
dump($this->request->param());
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号