Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL | E_STRICT);
- class Context implements ArrayAccess {
- /**
- * @var Context
- */
- protected $parent;
- /**
- * @var ArrayObject
- */
- protected $current;
- public function __construct($items = array(), Context $parent = NULL) {
- $this->current = new ArrayObject((array)$items);
- $this->parent = $parent;
- }
- protected function &getRef($index) {
- $ref = &$this->current[$index];
- if (!isset($ref) && ($this->parent !== NULL)) {
- $ref = &$this->parent->getRef($index);
- }
- return $ref;
- }
- public function offsetExists($index) {
- $ref = &$this->getRef($index);
- return isset($ref);
- }
- public function &offsetGet($index) {
- return $this->getRef($index);
- }
- public function offsetSet($index, $newval) {
- $ref = &$this->getRef($index);
- $ref = $newval;
- }
- public function offsetUnset($index) {
- $ref = &$this->getRef($index);
- unset($ref);
- }
- public function enter($items = array()) {
- return new static($items, $this);
- }
- public function leave() {
- return $this->parent;
- }
- }
- $context = new Context(array('a' => -10, 'b' => -20, 'c' => -30));
- $context = $context->enter(array('a' => 10, 'b' => 20));
- printf("%d\n", ++$context['a']);
- printf("%d\n", ++$context['c']);
- $context = $context->leave();
- printf("%d\n", ++$context['a']);
- printf("%d\n", ++$context['c']);
- $context['c'] = 6;
- printf("%d\n", $context['c']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement