“类包含1个抽象方法,因此必须声明为abstract或实现其余方法(parent :: hasErrors)”[关闭]

“类包含1个抽象方法,因此必须声明为abstract或实现其余方法(parent :: hasErrors)”[关闭]

问题描述:

This is my code:

interface ValidatorInterface
{
  public function hasErrors ();
  public function validate (stdClass $metadata);
  public function getFeedback ();
}

abstract class ValidatorAbstract implements ValidatorInterface
{
  protected $feedback;

  public function __construct ()
  {
    $this->feedback = new FormFeedback();
  }


  public function getFeedback ()
  {
    return $this->feedback;
  }


  public function hasErrors ()
  {
    return $this->getFeedback()->hasErrors();
  }
}

class RegistrationValidator extends ValidatorAbstract
{
  public function validate (stdClass $metadata)
  {
    $this->getFeedback()->addError('Testing');

    return !!$this->hasErrors();
  }
}

This is the error:

Fatal error: Class RegistrationValidator contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ValidatorInterface::hasErrors)

The abstract class ValidatorAbstract satisfies the requirements imposed by ValidatorInterface to have the hasErrors() and getFeedback() methods.

The class RegistrationValidator extends ValidatorAbstract and satisfies the last remaining requirement to declare a validate() method.

Why then am I getting an error saying that RegistrationValidator contains an abstract method, when it doesn't, and that it must implement hasErrors() when that method is already implemented by its parent class?

Make sure the code you posted is the same as the one you are running. In the example you posted, every abstract method is implemented somewhere in the hierarchy and the code runs fine.

Also make sure the file you are editing is the running copy (it could be cached).

In abstract class you need to have at least one abstract method.

abstract protected function someFunction();

A good practice is not to put any code into abstract classes.