gpt4 book ai didi

php - 某些类的访问级别必须是 PHP 中的公共(public)错误

转载 作者:可可西里 更新时间:2023-11-01 12:41:52 26 4
gpt4 key购买 nike

我创建了这个类

<?php
abstract class Validator{
public $_errors = array();
abstract public function isValid($input);

public function _addErrors($message){
$this->_errors = $message;
}

public function getErrors(){
return $this->_errors;
}


public function getMessage(){
return $this->message;
}
}

class Validator_NoSpaces extends Validator{

public function __construct($value){
$this->isValid($value);
}
public function isValid($value){
if (preg_match('/\s/', $value)){
$this->_addErrors("Spaces are not allowed");
return false;
}
return true;
}
}

class Validator_MinimumLength extends Validator{

protected $_minLength;
protected $value;

public function __construct($value ,$minLength=8){
$this->_minLength = $minLength;
$this->value = $value;
$this->isValid($value);
}

public function isValid($input){
if (strlen($input) > $this->_minLength) {
return true;
}else{
$this->_addErrors("Input must be at least {$this_minLength}");
return false;
}
}
}

class Form_Element_Validators extends Validator{

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->_addErrors($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->hasErrors();
}

}

class Form_Element extends Form_Element_Validators{

public function __construct($value){
$this->addValidator(new Validator_NoSpaces($value));
$this->addValidator(new Validator_MinimumLength($value));
}
}

出于验证目的,但它一直给我这个错误

Fatal error: Access level to Form_Element_Validators::_addErrors() must be public (as in class Validator) in C:\xampp\htdocs\beatbeast\includes\Db\Validators.php on line 91

但是这个类 $_errors 中的实例变量被声明为 public,我不明白为什么会收到这个错误。

最佳答案

你得到那个错误是因为方法的可见性必须与其在父类上的定义相同或更少限制。在这种情况下,您在抽象类上将 addErrors 作为 public 并试图使其在子类上 protected

关于php - 某些类的访问级别必须是 PHP 中的公共(public)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11183190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com