-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy patharray_access.phpt
More file actions
111 lines (98 loc) · 1.83 KB
/
array_access.phpt
File metadata and controls
111 lines (98 loc) · 1.83 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
--TEST--
Structs implementing ArrayAccess
--FILE--
<?php
abstract class VectorBase implements ArrayAccess {
public $elements;
public function offsetExists(mixed $offset): bool {
return array_key_exists($offset, $this->elements);
}
public function offsetSet(mixed $offset, mixed $value): void {
if ($offset) {
$this->elements[$offset] = $value;
} else {
$this->elements[] = $value;
}
}
public function offsetUnset(mixed $offset): void {
unset($this->elements[$offset]);
}
}
struct VectorByVal extends VectorBase {
public function offsetGet(mixed $offset): mixed {
return $this->elements[$offset];
}
}
struct VectorByRef extends VectorBase {
public function &offsetGet(mixed $offset): mixed {
return $this->elements[$offset];
}
}
struct Box {
public function __construct(
public int $value,
) {}
public mutating function inc() {
$this->value++;
}
}
$box = new Box(1);
$vec = new VectorByVal();
$vec[] = $box;
$vec[0]->value = 2;
var_dump($vec);
$vec[0]->inc!();
var_dump($vec);
$vec = new VectorByRef();
$vec[] = $box;
$vec[0]->value = 2;
var_dump($vec);
$vec[0]->inc!();
var_dump($vec);
var_dump($box);
?>
--EXPECT--
object(VectorByVal)#2 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#1 (1) {
["value"]=>
int(1)
}
}
}
object(VectorByVal)#2 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#1 (1) {
["value"]=>
int(1)
}
}
}
object(VectorByRef)#3 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#2 (1) {
["value"]=>
int(2)
}
}
}
object(VectorByRef)#3 (1) {
["elements"]=>
array(1) {
[0]=>
object(Box)#2 (1) {
["value"]=>
int(3)
}
}
}
object(Box)#1 (1) {
["value"]=>
int(1)
}