An error in the abstract must be compatible with

I have an abstract class, something like this

protected function update(Request $request, $id)
{
    //function body
}

      

and an extended class like

protected function update(PageRequest $request, $id)
{
    //function body
}

      

entered PageRequest

expanded from request

<?php

namespace App\Http\Requests;

use App\Helpers\Helpers;
use App\Http\Requests\Request;

class PageRequest extends Request
{
    //function body
}

      

I am getting this error

The application declaration \ Http \ Controllers \ PagesController :: update () must be compatible with App \ Http \ Controllers \ MasterController \ CrudController :: update (App \ Http \ Requests \ request $ request, $ id)

I know to pass all arguments and access them for update () methods and I think I am doing it right.

+3


source to share


2 answers


The correct way to do it is:

class Request implements RequestInterface
class PageRequest extends Request

      



And the function:

protected function update(RequestInterface $request, $id)

      

+4


source


The best solution is to use an interface as described in this answer .

Another solution is to declare the method update()

in the child class without changing its signature and check in the code that the argument is of the correct type.

Something like that:



protected function update(Request $request, $id)
{
    if (! $request instanceof PageRequest) {
        throw new \InvalidArgumentException('Unexpected type ('.get_class($request).') of argument $request. Expecting an object of type PageRequest.');
    }

    // Do whatever you need to do with $request here
    // It is an object of type PageRequest
}

      

This solution has its advantages in some contexts, but it makes the code more verbose and adds additional code to run.

+1


source







All Articles