Access Level to certain class must be public error in PHP

前端 未结 5 1579
猫巷女王i
猫巷女王i 2021-01-17 18:21

I created this class



        
相关标签:
5条回答
  • 2021-01-17 18:53

    Youre getting that error because the visibility of the method must be the same or less restrictive than that of it its definition on a parent class. In this case you have addErrors as public on your abstract class and are attempting to make it protected on a child class.

    0 讨论(0)
  • 2021-01-17 18:53

    Since PHP 7.2.0 there was a bug #61970 fixed (Restraining __construct() access level in subclass gives a fatal error).

    Bump your PHP version to 7.2 and you could make it private or protected.

    0 讨论(0)
  • 2021-01-17 18:54

    You've specify the protected access to the protected function _addErrors(array $errors) method of Form_Element_Validators class. So change it to public.

    Edit:

    Have you noticed? The sub class method (overridden method) is defined with Type Hinting. Please keep the same parameter type for both; super-class and sub-class method.

     abstract class Validator{
            public $_errors = array();
            abstract public function isValid($input);
    
            public function _addErrors(array $message){
                $this->_errors = $message;
            }
            ....
    
    0 讨论(0)
  • 2021-01-17 19:01

    It also happens when you are trying to do something with specific guard. You can add

     protected $guard = "guard_name";
    

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

    0 讨论(0)
  • 2021-01-17 19:04

    As others have mentioned, you can't make a sub class method more restrictive than the parent; this is because sub classes are supposed to be a valid replacement for their parent class.

    In your particular case, I would change the visibility of all methods and properties that start with an underscore to protected.

    0 讨论(0)
提交回复
热议问题