fork download
  1. <?php
  2.  
  3.  
  4. class MyClass
  5. {
  6. private $prop = "teste";
  7.  
  8. public function test()
  9. {
  10. $class = new class($this) {
  11.  
  12. public function __construct($obj)
  13. {
  14. $this->obj = $obj;
  15. }
  16.  
  17. public function run()
  18. {
  19. echo $this->obj->prop, PHP_EOL;
  20. }
  21.  
  22. };
  23.  
  24. $class->run();
  25. }
  26. }
  27.  
  28. $obj = new MyClass();
  29. $obj->test();
Runtime error #stdin #stdout #stderr 0.02s 82880KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  Uncaught Error: Cannot access private property MyClass::$prop in /home/WxhyCc/prog.php:19
Stack trace:
#0 /home/WxhyCc/prog.php(24): class@anonymous->run()
#1 /home/WxhyCc/prog.php(29): MyClass->test()
#2 {main}
  thrown in /home/WxhyCc/prog.php on line 19