Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: php-5.3.0alpha1
Fetching contributors…

Cannot retrieve contributors at this time

executable file 187 lines (168 sloc) 4.539 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
--TEST--
ZE2 ArrayAccess and ArrayAccessReferenceProxy with references to main array
--FILE--
<?php

// NOTE: This will become part of SPL

class ArrayAccessReferenceProxy implements ArrayAccess
{
private $object;
private $oarray;
private $element;

function __construct(ArrayAccess $object, array &$array, $element)
{
echo __METHOD__ . "($element)\n";
$this->object = $object;
$this->oarray = &$array;
$this->element = $element;
}

function offsetExists($index) {
echo __METHOD__ . "($this->element, $index)\n";
return array_key_exists($index, $this->oarray[$this->element]);
}

function offsetGet($index) {
echo __METHOD__ . "($this->element, $index)\n";
return isset($this->oarray[$this->element][$index]) ? $this->oarray[$this->element][$index] : NULL;
}

function offsetSet($index, $value) {
echo __METHOD__ . "($this->element, $index, $value)\n";
$this->oarray[$this->element][$index] = $value;
}

function offsetUnset($index) {
echo __METHOD__ . "($this->element, $index)\n";
unset($this->oarray[$this->element][$index]);
}
}

class Peoples implements ArrayAccess
{
public $person;

function __construct()
{
$this->person = array(array('name'=>'Foo'));
}

function offsetExists($index)
{
return array_key_exists($index, $this->person);
}

function offsetGet($index)
{
if (is_array($this->person[$index]))
{
return new ArrayAccessReferenceProxy($this, $this->person, $index);
}
else
{
return $this->person[$index];
}
}

function offsetSet($index, $value)
{
$this->person[$index] = $value;
}

function offsetUnset($index)
{
unset($this->person[$index]);
}
}

$people = new Peoples;

var_dump($people->person[0]['name']);
$people->person[0]['name'] = $people->person[0]['name'] . 'Bar';
var_dump($people->person[0]['name']);
$people->person[0]['name'] .= 'Baz';
var_dump($people->person[0]['name']);

echo "===ArrayOverloading===\n";

$people = new Peoples;

var_dump($people[0]);
var_dump($people[0]['name']);
$people[0]['name'] = 'FooBar';
var_dump($people[0]['name']);
$people[0]['name'] = $people->person[0]['name'] . 'Bar';
var_dump($people[0]['name']);
$people[0]['name'] .= 'Baz';
var_dump($people[0]['name']);
unset($people[0]['name']);
var_dump($people[0]);
var_dump($people[0]['name']);
$people[0]['name'] = 'BlaBla';
var_dump($people[0]['name']);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
string(3) "Foo"
string(6) "FooBar"
string(9) "FooBarBaz"
===ArrayOverloading===
ArrayAccessReferenceProxy::__construct(0)
object(ArrayAccessReferenceProxy)#%d (3) {
["object":"ArrayAccessReferenceProxy":private]=>
object(Peoples)#%d (1) {
["person"]=>
&array(1) {
[0]=>
array(1) {
["name"]=>
string(3) "Foo"
}
}
}
["oarray":"ArrayAccessReferenceProxy":private]=>
&array(1) {
[0]=>
array(1) {
["name"]=>
string(3) "Foo"
}
}
["element":"ArrayAccessReferenceProxy":private]=>
int(0)
}
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetGet(0, name)
string(3) "Foo"
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetSet(0, name, FooBar)
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetGet(0, name)
string(6) "FooBar"
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetSet(0, name, FooBarBar)
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetGet(0, name)
string(9) "FooBarBar"
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetGet(0, name)
ArrayAccessReferenceProxy::offsetSet(0, name, FooBarBarBaz)
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetGet(0, name)
string(12) "FooBarBarBaz"
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetUnset(0, name)
ArrayAccessReferenceProxy::__construct(0)
object(ArrayAccessReferenceProxy)#%d (3) {
["object":"ArrayAccessReferenceProxy":private]=>
object(Peoples)#%d (1) {
["person"]=>
&array(1) {
[0]=>
array(0) {
}
}
}
["oarray":"ArrayAccessReferenceProxy":private]=>
&array(1) {
[0]=>
array(0) {
}
}
["element":"ArrayAccessReferenceProxy":private]=>
int(0)
}
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetGet(0, name)
NULL
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetSet(0, name, BlaBla)
ArrayAccessReferenceProxy::__construct(0)
ArrayAccessReferenceProxy::offsetGet(0, name)
string(6) "BlaBla"
===DONE===
Something went wrong with that request. Please try again.