Php – Call a protected method from outside a class in PHP


I have a very special case in which I need to call a protected method from outside a class. I am very conscious about what I do programmingwise, but I would not be entirely opposed to doing so in this one special case I have. In all other cases, I need to continue disallowing access to the internal method, and so I would like to keep the method protected.

What are some elegant ways to access a protected method outside of a class? So far, I've found this.

I suppose it may be possible create some kind of double-agent instance of the target class that would sneakily provide access to the internals…

Best Answer

In PHP you can do this using Reflections. To invoke protected or private methods use the setAccessible() method (just set it to TRUE)