There is an object of a certain class "A", you need to display its property and call a method.

php gives the error that there is no access to these methods. they are pretected.

how to get access?

  • As I understand it, create your own class and inherit from class "A", but what to do next? - aat
  • continue to use your new class in code, and define in it the method that calls your protected method. - Dmitry Gvozd

1 answer 1

You have several options.

  • 1) inheritance (create a new class inheriting the base class). With inheritance, you can do "anything", override methods, call protected, etc.
  • 2) use the reflection. With reflection, you can get the names of the methods and properties but all the same you cannot call the protected method.
  • 3) use one of the design patterns for such tasks, trying to remember its name ....
  • Thanks, I will try to describe the situation: There is an event function before ($ obj) {} The $ obj parameter is an object of the Events class that has a result (array) field - protected. need to access it in the event. I'm trying to inherit: class myClass extends Events {public function A () {print_r ($ this-> result); }} $ q = new myClass; $ q-> A (); Naturally displays an empty result, since I refer directly to the class property not to the property of the object. How to address the object? - aat
  • in the comments such things are not taken to describe. Update the keypad with an example and issue the code as it is for plz. - Dmitry Gvozd