我在这里创建了一个包含抽象方法的类,即使该方法被声明为抽象,它也总是将这个错误返回给我。这是我的整个代码
<?php
interface Validator{
public function isValid($input);
public function getErrors();
}
class Validator_AbstractValidator
{
protected $_errors = array();
abstract public function isValid($input);
public function getErrors()
{
return $this->_errors;
}
protected function _addError($message){
$this_errors[] = $message;
}
}
class Validator_MinimumLength extends Validator_AbstractValidator
{
protected $_minLength;
public function __construct($minLength)
{
$this_minLength = $minLength;
}
public function isValid($input){
if (strlen($input) > $this->_minLength) {
return true;
} else {
$this->_addError("Input must be at least {$this_minLength}");
return false;
}
}
}
class Validator_NoSpaces extends Validator_AbstractValidator{
public function isValid($input) {
if (preg_match('/\s/', $input)){
$this->_addError("Spaces are not allowed");
return false;
}
return true;
}
}
interface Form_ElementInterface extends Validator{ }
class Form_Element_AbstractElement extends Validator_AbstractValidator implements Form_ElementInterface
{
protected $_validators = array();
public function addValidator(Validator $validator)
{
$this->_validators[] = $validator;
}
public function getValidators()
{
return $this->_validators;
}
protected function _addErrors(array $errors)
{
foreach ($errors as $error) {
$this->_addError($error);
}
}
public function hasErrors()
{
return (count($this->getErrors()) !== 0);
}
public function isValid($input)
{
foreach ($this->_validators as $validator) {
if (!$validator->isValid($input)) {
$this->_addErrors($validator->getErrors());
}
}
return !$this->hadErrors();
}
}
class Form_Element extends Form_Element_AbstractElement
{
public function __construct($value,$minLength = 8)
{
$this->addValidator(new Validator_NoSpaces($value));
$this->addValidator(new Validator_MinimumLength($minLength));
//...
}
}
错误是这个致命错误:类 Validator_AbstractValidator 包含 1 个抽象方法,因此必须在第 21 行的 C:\xampp\htdocs\beatbeast\includes\Db\Validator.php 中声明为抽象方法或实现其余方法 (Validator_AbstractValidator::isValid)
但第 21 行只包含这个
}
有什么我错过的吗?
即使我在 Class Validator_AbstractValidator 类中添加了关键抽象,我也遇到了这个问题
Fatal error: Can't inherit abstract function Validator::isValid() (previously declared abstract in Validator_AbstractValidator) in C:\xampp\htdocs\beatbeast\includes\Db\Validator.php on line 57
但是第 57 行再次只包含这个
{