xref: /PHP-8.4/Zend/tests/gh14969.phpt (revision 8c312ba7)
1--TEST--
2GH-14969: Crash on coercion with throwing __toString()
3--FILE--
4<?php
5
6class C {
7    public function __toString() {
8        global $c;
9        $c = [];
10        throw new Exception(__METHOD__);
11    }
12}
13
14class D {
15    public string $prop;
16}
17
18$c = new C();
19$d = new D();
20try {
21    $d->prop = $c;
22} catch (Throwable $e) {
23    echo $e->getMessage(), "\n";
24}
25var_dump($d);
26
27$c = new C();
28$d->prop = 'foo';
29try {
30    $d->prop = $c;
31} catch (Throwable $e) {
32    echo $e->getMessage(), "\n";
33}
34var_dump($d);
35
36?>
37--EXPECTF--
38C::__toString
39object(D)#%d (0) {
40  ["prop"]=>
41  uninitialized(string)
42}
43C::__toString
44object(D)#2 (1) {
45  ["prop"]=>
46  string(3) "foo"
47}
48