How to call a protected method in php?

前端 未结 4 2063
清酒与你
清酒与你 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条回答
  •  终归单人心
    2021-01-22 15:07

    protected means that you can call that method only from the same class and from subclasses. What you want to do is not possible. The protected keyword would be pointless if you could call these methods from everywhere.

    In C++ there is the friend keyword to achieve what you want: you could define Child as friend of Observer (this has to be done from within Observer), and then you can call all methods in Observer (including private and protected) from within methods of Child. But such a keyword does not exist for PHP.

提交回复
热议问题