class MyClass { private $myProp; } $obj = new MyClass(); $refProp = new ReflectionProperty('MyClass', 'myProp'); $refProp->setAccessible(true); $refProp->setValue($obj, 'new value');
class MyClass { public $prop1; public $prop2; } $obj = new MyClass(); $prop1Ref = new ReflectionProperty('MyClass', 'prop1'); $prop2Ref = new ReflectionProperty('MyClass', 'prop2'); $prop1Ref->setValue($obj, 'new value 1'); $prop2Ref->setValue($obj, 'new value 2');In this example, we create an object of `MyClass` with two public properties, `prop1` and `prop2`. We then use reflection to access each property and set a new value for each one. The package/library for these examples would be the core PHP language itself, as the `ReflectionProperty` class is part of the core reflection API.