dereference_005.phpt 877 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. --TEST--
  2. Testing array dereference on object that implements ArrayAccess
  3. --FILE--
  4. <?php
  5. error_reporting(E_ALL);
  6. class obj implements arrayaccess {
  7. private $container = array();
  8. public function __construct() {
  9. $this->container = array(
  10. "one" => 1,
  11. "two" => 2,
  12. "three" => 3,
  13. );
  14. }
  15. public function offsetSet($offset, $value): void {
  16. $this->container[$offset] = $value;
  17. }
  18. public function offsetExists($offset): bool {
  19. return isset($this->container[$offset]);
  20. }
  21. public function offsetUnset($offset): void {
  22. unset($this->container[$offset]);
  23. }
  24. public function offsetGet($offset): mixed {
  25. return isset($this->container[$offset]) ? $this->container[$offset] : null;
  26. }
  27. }
  28. function x() {
  29. return new obj;
  30. }
  31. var_dump(x()['two']);
  32. ?>
  33. --EXPECT--
  34. int(2)