fork download
  1. <?php
  2.  
  3. class MyArray implements ArrayAccess {
  4. private $data = array ();
  5. private function realOffset($offset) {
  6. return $offset + count($this->data) * intval($offset < 0);
  7. }
  8. public function offsetGet ($offset) {
  9. return $this->data[$this->realOffset($offset)];
  10. }
  11. public function offsetExists ($offset) {
  12. return array_key_exists( $offset, $this->data)
  13. || array_key_exists(~$offset, $this->data);
  14. }
  15. public function offsetSet ($offset, $value) {
  16. $this->data[$this->realOffset($offset)] = $value;
  17. }
  18. public function offsetUnset ($offset) {
  19. unset($this->data[$this->realOffset($offset)]);
  20. }
  21. public function __construct() {
  22. $this->data = func_get_args();
  23. }
  24. }
  25.  
  26. $m = new MyArray(4, 5, 6);
  27. echo sprintf("m[0] =%d, m[1] =%d, m[2] =%d\n", $m[0], $m[1], $m[2]);
  28. echo sprintf("m[~0]=%d, m[~1]=%d, m[~2]=%d\n", $m[~0], $m[~1], $m[~2]);
  29.  
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
m[0] =4, m[1] =5, m[2] =6
m[~0]=6, m[~1]=5, m[~2]=4