004.phpt 899 B

12345678910111213141516171819202122232425262728293031
  1. --TEST--
  2. testing anonymous inheritance
  3. --FILE--
  4. <?php
  5. class Outer {
  6. protected $data;
  7. public function __construct($data) {
  8. $this->data = $data;
  9. }
  10. public function getArrayAccess() {
  11. /* create a proxy object implementing array access */
  12. return new class($this->data) extends Outer implements ArrayAccess {
  13. public function offsetGet($offset): mixed { return $this->data[$offset]; }
  14. public function offsetSet($offset, $data): void { $this->data[$offset] = $data; }
  15. public function offsetUnset($offset): void { unset($this->data[$offset]); }
  16. public function offsetExists($offset): bool { return isset($this->data[$offset]); }
  17. };
  18. }
  19. }
  20. $outer = new Outer(array(
  21. rand(1, 100)
  22. ));
  23. /* not null because inheritance */
  24. var_dump($outer->getArrayAccess()[0]);
  25. ?>
  26. --EXPECTF--
  27. int(%d)