admin管理员组文章数量:1355554
I am trying to use the implements
of PHP where the parameter is a base/generic type class.
BaseFormRequest.php
class BaseFormRequest extends FormRequest
{
... methods here
}
StorePostRequest.php
class StorePostRequest extends BaseFormRequest
{
... methods here
}
IController.php
interface IController
{
public function store(BaseFormRequest $request);
}
class PostController extends Controller implements IController
{
public $service;
public function __construct(PostService $service)
{
$this->service = $service;
}
public function store(StorePostRequest $request)
{
$post = $this->service->store($request);
}
}
As you can see in the code:
- in StorePostRequest, it extends the BaseFormRequest.
- in the IController, the parameter in store method is type BaseFormRequest.
- in PostController, the parameter in store method is type StorePostRequest.
I am getting the error
Declaration of PostController::store(StorePostRequest $request) must be compatible with IController::store(\App\Http\Requests\BaseFormRequest $request)
What I do know is that
- if I remove the implements IController, the code works as intended
- if I change the PostController@store param to BaseFormRequest, or IController@store param to StorePostRequest the error is fixed, but I cant customize the rules in each model this way.
Is it not possible to use a generic class in interface so that when I implement it in my controller, I can use the correct class that extends to the generic class?
本文标签: phpUsing a basegeneric type class as a parameter in a methodStack Overflow
版权声明:本文标题:php - Using a basegeneric type class as a parameter in a method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743999236a2573552.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论