File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / tests / classes / array_access_009.phpt
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 23:48:06 2012 UTC (12 years, 5 months ago) by misho
Branches: php, MAIN
CVS tags: v5_4_3elwix, v5_4_29p0, v5_4_29, v5_4_20p0, v5_4_20, v5_4_17p0, v5_4_17, v5_3_10, HEAD
php

    1: --TEST--
    2: ZE2 ArrayAccess and ArrayProxyAccess, ArrayProxy
    3: --FILE--
    4: <?php 
    5: 
    6: // NOTE: This will become part of SPL
    7: 
    8: interface ArrayProxyAccess extends ArrayAccess
    9: {
   10: 	function proxyGet($element);
   11: 	function proxySet($element, $index, $value);
   12: 	function proxyUnset($element, $index);
   13: }
   14: 
   15: class ArrayProxy implements ArrayAccess
   16: {
   17: 	private $object;
   18: 	private $element;
   19: 	
   20: 	function __construct(ArrayProxyAccess $object, $element)
   21: 	{
   22: 		echo __METHOD__ . "($element)\n";
   23: 		if (!$object->offsetExists($element))
   24: 		{
   25: 			$object[$element] = array();
   26: 		}
   27: 		$this->object = $object;
   28: 		$this->element = $element;
   29: 	}
   30: 
   31: 	function offsetExists($index) {
   32: 		echo __METHOD__ . "($this->element, $index)\n";
   33: 		return array_key_exists($index, $this->object->proxyGet($this->element));
   34: 	}
   35: 
   36: 	function offsetGet($index) {
   37: 		echo __METHOD__ . "($this->element, $index)\n";
   38: 		$tmp = $this->object->proxyGet($this->element);
   39: 		return isset($tmp[$index]) ? $tmp[$index] : NULL;
   40: 	}
   41: 
   42: 	function offsetSet($index, $value) {
   43: 		echo __METHOD__ . "($this->element, $index, $value)\n";
   44: 		$this->object->proxySet($this->element, $index, $value);
   45: 	}
   46: 
   47: 	function offsetUnset($index) {
   48: 		echo __METHOD__ . "($this->element, $index)\n";
   49: 		$this->object->proxyUnset($this->element, $index);
   50: 	}
   51: }
   52: 
   53: class Peoples implements ArrayProxyAccess
   54: {
   55: 	public $person;
   56: 	
   57: 	function __construct()
   58: 	{
   59: 		$this->person = array(array('name'=>'Foo'));
   60: 	}
   61: 
   62: 	function offsetExists($index)
   63: 	{
   64: 		return array_key_exists($index, $this->person);
   65: 	}
   66: 
   67: 	function offsetGet($index)
   68: 	{
   69: 		return new ArrayProxy($this, $index);
   70: 	}
   71: 
   72: 	function offsetSet($index, $value)
   73: 	{
   74: 		$this->person[$index] = $value;
   75: 	}
   76: 
   77: 	function offsetUnset($index)
   78: 	{
   79: 		unset($this->person[$index]);
   80: 	}
   81: 
   82: 	function proxyGet($element)
   83: 	{
   84: 		return $this->person[$element];
   85: 	}
   86: 
   87: 	function proxySet($element, $index, $value)
   88: 	{
   89: 		$this->person[$element][$index] = $value;
   90: 	}
   91: 	
   92: 	function proxyUnset($element, $index)
   93: 	{
   94: 		unset($this->person[$element][$index]);
   95: 	}
   96: }
   97: 
   98: $people = new Peoples;
   99: 
  100: var_dump($people->person[0]['name']);
  101: $people->person[0]['name'] = $people->person[0]['name'] . 'Bar';
  102: var_dump($people->person[0]['name']);
  103: $people->person[0]['name'] .= 'Baz';
  104: var_dump($people->person[0]['name']);
  105: 
  106: echo "===ArrayOverloading===\n";
  107: 
  108: $people = new Peoples;
  109: 
  110: var_dump($people[0]);
  111: var_dump($people[0]['name']);
  112: $people[0]['name'] = 'FooBar';
  113: var_dump($people[0]['name']);
  114: $people[0]['name'] = $people->person[0]['name'] . 'Bar';
  115: var_dump($people[0]['name']);
  116: $people[0]['name'] .= 'Baz';
  117: var_dump($people[0]['name']);
  118: unset($people[0]['name']);
  119: var_dump($people[0]);
  120: var_dump($people[0]['name']);
  121: $people[0]['name'] = 'BlaBla';
  122: var_dump($people[0]['name']);
  123: 
  124: ?>
  125: ===DONE===
  126: --EXPECTF--
  127: string(3) "Foo"
  128: string(6) "FooBar"
  129: string(9) "FooBarBaz"
  130: ===ArrayOverloading===
  131: ArrayProxy::__construct(0)
  132: object(ArrayProxy)#%d (2) {
  133:   ["object":"ArrayProxy":private]=>
  134:   object(Peoples)#%d (1) {
  135:     ["person"]=>
  136:     array(1) {
  137:       [0]=>
  138:       array(1) {
  139:         ["name"]=>
  140:         string(3) "Foo"
  141:       }
  142:     }
  143:   }
  144:   ["element":"ArrayProxy":private]=>
  145:   int(0)
  146: }
  147: ArrayProxy::__construct(0)
  148: ArrayProxy::offsetGet(0, name)
  149: string(3) "Foo"
  150: ArrayProxy::__construct(0)
  151: ArrayProxy::offsetSet(0, name, FooBar)
  152: ArrayProxy::__construct(0)
  153: ArrayProxy::offsetGet(0, name)
  154: string(6) "FooBar"
  155: ArrayProxy::__construct(0)
  156: ArrayProxy::offsetSet(0, name, FooBarBar)
  157: ArrayProxy::__construct(0)
  158: ArrayProxy::offsetGet(0, name)
  159: string(9) "FooBarBar"
  160: ArrayProxy::__construct(0)
  161: ArrayProxy::offsetGet(0, name)
  162: ArrayProxy::offsetSet(0, name, FooBarBarBaz)
  163: ArrayProxy::__construct(0)
  164: ArrayProxy::offsetGet(0, name)
  165: string(12) "FooBarBarBaz"
  166: ArrayProxy::__construct(0)
  167: ArrayProxy::offsetUnset(0, name)
  168: ArrayProxy::__construct(0)
  169: object(ArrayProxy)#%d (2) {
  170:   ["object":"ArrayProxy":private]=>
  171:   object(Peoples)#%d (1) {
  172:     ["person"]=>
  173:     array(1) {
  174:       [0]=>
  175:       array(0) {
  176:       }
  177:     }
  178:   }
  179:   ["element":"ArrayProxy":private]=>
  180:   int(0)
  181: }
  182: ArrayProxy::__construct(0)
  183: ArrayProxy::offsetGet(0, name)
  184: NULL
  185: ArrayProxy::__construct(0)
  186: ArrayProxy::offsetSet(0, name, BlaBla)
  187: ArrayProxy::__construct(0)
  188: ArrayProxy::offsetGet(0, name)
  189: string(6) "BlaBla"
  190: ===DONE===

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>