How do I call a protected function?

How to call a function lmn()

without touching a class B

from within a classA

 class A extends B{
     public function abc(){
         return "abc";
     }
     ...
 }

 class B{
     public function xyz(){
         return "xyz";
     }
     ...
 }

 class C{
     protected function lmn(){
         return "lmn";
     }
     ...
 }

      

please call me for this

+3


source to share


2 answers


You can create some kind of proxy class that extends from C and provides a public accessor:



class ProcyForC extends C {
  public function getLmn() {
    return $this->lmn();
  }
}

echo (new ProxyForC())->getLmn();

      

+1


source


You cannot call it as protected means a function that can be called from child classes.



In your case, you need to make an instance of B to call lmn in any class.

-1


source







All Articles