Access Level to certain class must be public error in PHP

你离开我真会死。 提交于 2019-12-01 16:32:47

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.

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.

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;
        }
        ....

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!