005.phpt 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. --TEST--
  2. testing reusing anons that implement an interface
  3. --FILE--
  4. <?php
  5. class Outer {
  6. protected $data;
  7. public function __construct(&$data) {
  8. /* array access will be implemented by the time we get to here */
  9. $this->data = &$data;
  10. }
  11. public function getArrayAccess() {
  12. /* create a child object implementing array access */
  13. /* this grants you access to protected methods and members */
  14. return new class($this->data) implements ArrayAccess {
  15. public function offsetGet($offset): mixed { return $this->data[$offset]; }
  16. public function offsetSet($offset, $data): void { $this->data[$offset] = $data; }
  17. public function offsetUnset($offset): void { unset($this->data[$offset]); }
  18. public function offsetExists($offset): bool { return isset($this->data[$offset]); }
  19. };
  20. }
  21. }
  22. $data = array(
  23. rand(1, 100),
  24. rand(2, 200)
  25. );
  26. $outer = new Outer($data);
  27. $proxy = $outer->getArrayAccess();
  28. /* null because no inheritance, so no access to protected member */
  29. var_dump(@$outer->getArrayAccess()[0]);
  30. ?>
  31. --EXPECT--
  32. NULL