fork download
  1. <?php
  2.  
  3. class Foo {
  4. public function __construct($prop) {
  5. $this->prop = $prop;
  6. }
  7.  
  8. public function __toString() {
  9. return $this->prop;
  10. }
  11. }
  12.  
  13. class MyClass
  14. {
  15. public function __construct()
  16. {
  17. $this->prop = new Foo("test");
  18. }
  19.  
  20. public function test()
  21. {
  22. $class = new class($this->prop) {
  23.  
  24. public function __construct($prop)
  25. {
  26. $this->prop = $prop;
  27. }
  28.  
  29. public function run()
  30. {
  31. $this->prop->prop = "Teste na classe anĂ´nima";
  32. echo $this->prop, PHP_EOL;
  33. }
  34.  
  35. };
  36.  
  37. $class->run();
  38. }
  39. }
  40.  
  41. $obj = new MyClass();
  42. $obj->test();
  43.  
  44. echo $obj->prop, PHP_EOL;
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
Teste na classe anônima
Teste na classe anônima