summaryrefslogtreecommitdiff
path: root/Zend/tests/type_declarations/typed_properties_034.phpt
blob: 4af6baa80aef36cf0ac33006203b2d0182379044 (plain)
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
--TEST--
Test typed properties passed to typed function
--FILE--
<?php
$foo = new class {
    public ?int $bar = 42;
    public int $baz;

    public function &getIterator() {
        foreach (['1', &$this->bar] as &$item) {
            yield $item;
        }
    }
};

function foo(?int &$a) {
    var_dump($a);
    $a = null;
}

foo($foo->bar);

try {
    $foo->baz = &$foo->bar;
} catch (Error $e) { echo $e->getMessage(), "\n"; }
$foo->bar = 10;

foreach ($foo->getIterator() as &$item) {
    $foo->baz = &$item;
    var_dump($foo->baz);
}

try {
    foo($foo->bar);
} catch (Error $e) { echo $e->getMessage(), "\n"; }

var_dump($foo);
?>
--EXPECT--
int(42)
Cannot assign null to property class@anonymous::$baz of type int
int(1)
int(10)
int(10)
Cannot assign null to reference held by property class@anonymous::$baz of type int
object(class@anonymous)#1 (2) {
  ["bar"]=>
  &int(10)
  ["baz"]=>
  &int(10)
}