0

Is there a way to access and protected property of an object without changing the class in PHP.

Is there maybe a workaround to access protected properties? currently i am able to get the

<?php class MyClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; } $item = new MyClass(); echo $item->protected; //Obviously this doesn't work, but to demonstrate what i need 

Currently I don't have the option to change or extend the class, is there a workaround to still get the data stored in this property?

If there isn’t a way i will delete my post and look for another solution

5
  • Why is the property protected if you need to be able to access it? Commented Sep 29, 2022 at 15:17
  • You could with Reflection. But you should not. It has a reason it is proteced. Commented Sep 29, 2022 at 15:18
  • And what is to stop you extending the class? Commented Sep 29, 2022 at 15:20
  • I don’t know exactly why, the informatie is not classified, I would have to ask the developers that made the software this question. Commented Sep 29, 2022 at 15:20
  • Good for me to add this to the original question, but the software is made and maintained by another company, I can only access a few classes, theoretically i could change the core of the application, but i am afraid that in the future with another update my changes would revert. Commented Sep 29, 2022 at 15:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.