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
|
<?php
declare(strict_types=1);
####################################
# Q1
$x = 1;
$y =& $x;
$y = 2;
echo "x = $x", PHP_EOL;
// => 2
echo "y = $y", PHP_EOL;
// => 2
####################################
# Q2
$x = 1;
$y =& $x;
$z = $y;
$z = 2;
echo "x = $x", PHP_EOL;
// => 1
echo "y = $y", PHP_EOL;
// => 1
echo "z = $z", PHP_EOL;
// => 2
####################################
# Q3
$xs = [1, 2];
$x =& $xs[0];
$x = 42;
echo "x = $x", PHP_EOL;
// => 42
echo "xs = [$xs[0], $xs[1]]", PHP_EOL;
// => [42, 2]
####################################
# Q4
$xs = [1, 2];
$x =& $xs[0];
$ys = $xs;
$x = 42;
$ys[1] = 3;
echo "x = $x", PHP_EOL;
// => 42
echo "xs = [$xs[0], $xs[1]]", PHP_EOL;
// => [42, 2]
echo "ys = [$ys[0], $ys[1]]", PHP_EOL;
// => [42, 3]
####################################
# Q5 (割愛)
$g = 1;
function f(&$x) {
$x =& $GLOBALS['g'];
}
$y = 0;
f($y);
$y = 42;
echo "y = $y", PHP_EOL;
// => 42
echo "g = $g", PHP_EOL;
// => 1
####################################
# Q6 (割愛)
class C {
public int $x = 1;
}
$c = new C();
$y =& $c->x;
$y = 'PHPerKaigi';
// => Fatal error: TypeError
// Cannot assign string to reference held by property C::$x of type int
|