How to call a protected method in php?

前端 未结 4 2064
清酒与你
清酒与你 2021-01-22 14:28

here is the class structure. I want Observer:callme() to be callable from Children too.

class Observer
{
    protected callme()
    {
    }
}

class Parent exten         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 14:54

    The problem is that a protected method is only accessed from the same class or the class children. What you can do is extend your Child class from Parent, like this:

    class Child extends Parent
    {
        public function __constructor ()
        {
            parent::__constructor();
        }
    
        public function __destroy()
        {
            $this->callme(); // Should work!
        }
    }
    

    Or just change the method to public.

    And, btw, is this code some kind of real code that you will use? That constructor receiving the parent object seems to be so wrong. What are you trying to accomplish?

提交回复
热议问题