Guest User

character_access.phpt

a guest
Mar 20th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. --TEST--
  2. test __toString character access vs ArrayAccess
  3. --FILE--
  4. <?php
  5. class Test implements ArrayAccess {
  6.     private $items = [];
  7.     public function offsetExists($offset) {
  8.         return array_key_exists($offset, $this->items);
  9.     }
  10.     public function offsetSet($offset, $value) {
  11.         if ($offset === NULL) {
  12.             $this->items[] = $value;
  13.         } else {
  14.             $this->items[$offset] = $value;
  15.         }
  16.     }
  17.     public function offsetUnset($offset) {
  18.         unset($this->items[$offset]);
  19.     }
  20.     public function offsetGet($offset) {
  21.         return $this->offsetExists($offset) ? $this->items[$offset] : NULL;
  22.     }
  23.     public function __toString() {
  24.         return 'TEST';
  25.     }
  26. }
  27. $test = new Test;
  28. $test[] = 'One';
  29. $test[] = 'Two';
  30. $test[] = 'Three';
  31. var_dump($test[2]);
  32. var_dump($test{2});
  33. ?>
  34. --EXPECT--
  35. string(5) "Three"
  36. string(1) "S"
Advertisement
Add Comment
Please, Sign In to add comment