class MyClass { private $name = "John Doe"; } $myObj = new MyClass(); $refObj = new ReflectionObject($myObj); $refProp = $refObj->getProperty("name"); $refProp->setAccessible(true); echo $refProp->getValue($myObj); // Output: John Doe
class MyClass { protected $age = 30; } $myObj = new MyClass(); $refObj = new ReflectionObject($myObj); $refProp = $refObj->getProperty("age"); $refProp->setAccessible(true); $refProp->setValue($myObj, 35); echo $myObj->age; // Output: 35In this example, the protected property 'age' of the 'MyClass' object is accessed and modified using the reflection property 'setAccessible' and 'setValue' methods. The package library for the ReflectionProperty class is 'Reflection'.