summaryrefslogtreecommitdiff
path: root/Zend/tests/assign_coalesce_002.phpt
blob: e49544effc0e589185ba0c12274fd758a44e5f57 (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
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
--TEST--
Coalesce assign (??=): Exception handling
--FILE--
<?php

$foo = "fo";
$foo .= "o";
$bar = "ba";
$bar .= "r";

function id($arg) {
    echo "id($arg)\n";
    return $arg;
}

function do_throw($msg) {
    throw new Exception($msg);
}

$ary = [];
try {
    $ary[id($foo)] ??= do_throw("ex1");
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}
var_dump($ary);

class AA implements ArrayAccess {
    public function offsetExists($k) {
        return true;
    }
    public function &offsetGet($k) {
        $var = ["foo" => "bar"];
        return $var;
    }
    public function offsetSet($k,$v) {}
    public function offsetUnset($k) {}
}

class Dtor {
    public function __destruct() {
        throw new Exception("dtor");
    }
}

$ary = new AA;
try {
    $ary[new Dtor][id($foo)] ??= $bar;
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}
var_dump($foo);

class AA2 implements ArrayAccess {
    public function offsetExists($k) {
        return false;
    }
    public function offsetGet($k) {
        return null;
    }
    public function offsetSet($k,$v) {}
    public function offsetUnset($k) {}
}

$ary = ["foo" => new AA2];
try {
    $ary[id($foo)][new Dtor] ??= $bar;
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}
var_dump($foo);

?>
--EXPECT--
id(foo)
ex1
array(0) {
}
id(foo)
dtor
string(3) "foo"
id(foo)
dtor
string(3) "foo"