The level of access to a particular class must be a public error in PHP

I created this class

<?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));
        }
    }

for verification purposes but he kept giving me this error

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

But the instance variable in this class $ _errors is declared public, I don’t understand why I get this error.

+7
source share
5 answers

You get this error because the visibility of the method must be the same or less restrictive than its definition in the parent class. In this case, you have addErrorsboth publicin your abstract class and try to make it protectedin a child class.

+15
source

, , ; , .

, , protected.

+9

protected protected function _addErrors(array $errors) Form_Element_Validators. .

Edit:

? ( ) Type Hinting. , ; .

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

        public function _addErrors(array $message){
            $this->_errors = $message;
        }
        ....
+2

PHP 7.2.0, № 61970 ( __construct() ).

PHP 7.2, private protected.

0

This also happens when you try to do something with a specific guard. You can add

 protected $guard = "guard_name";

Add the line above in your model. this will solve the problem.

0
source

All Articles