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

Bart
  • 1
  • 3
  • Why is the property protected if you need to be able to access it? – Barmar Sep 29 '22 at 15:17
  • You could with Reflection. But you should not. It has a reason it is proteced. – Markus Zeller Sep 29 '22 at 15:18
  • And what is to stop you extending the class? – RiggsFolly Sep 29 '22 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. – Bart Sep 29 '22 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. – Bart Sep 29 '22 at 15:22

0 Answers0