E
E
ezayka2018-09-10 18:39:33
PHP
ezayka, 2018-09-10 18:39:33

How to access a protected method of another class?

I want to use in my class a method of another class whose scope is protected. I do not inherit from him.
What's the best way to do it here? I suspect that changing the scope to public is not good.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2018-09-10
@Denormalization

If you don’t want to get into someone else’s code and change it, and even more so do intimate things through Reflection, then it’s best to create your own class inherited from the right one, and add a public method in your class that will call the protected ancestor.
Thus, it will not be necessary to change someone else's class, while maintaining access to all the functionality of the base class.

P
Pavlo Ponomarenko, 2018-09-10
@TheShock

In fact, "change to public" is the only correct solution. And it’s even better to put the responsibility of this method in a separate class, since so many classes already depend on it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question