fork download
  1. <?php
  2.  
  3. class Foo
  4. {
  5. private $property = 'value';
  6.  
  7. public function __get(string $name)
  8. {
  9. throw new LogicException('Forbidden');
  10. }
  11.  
  12. public function unsetProperty()
  13. {
  14. unset($this->property);
  15. }
  16. }
  17.  
  18. $object = new Foo();
  19. $reflectionProperty = new ReflectionProperty($object, 'property');
  20. $reflectionProperty->setAccessible(true);
  21.  
  22. var_dump($reflectionProperty->getValue($object));
  23. $object->unsetProperty();
  24. var_dump($reflectionProperty->getValue($object));
Runtime error #stdin #stdout #stderr 0.02s 82880KB
stdin
Standard input is empty
stdout
string(5) "value"
stderr
PHP Fatal error:  Uncaught LogicException: Forbidden in /home/DbiWOH/prog.php:9
Stack trace:
#0 [internal function]: Foo->__get('property')
#1 /home/DbiWOH/prog.php(24): ReflectionProperty->getValue(Object(Foo))
#2 {main}
  thrown in /home/DbiWOH/prog.php on line 9