1--TEST--
2Test exceptions thrown from __toString() in various contexts
3--FILE--
4<?php
5
6class BadStr {
7    public function __toString() {
8        throw new Exception("Exception");
9    }
10}
11
12$str = "a";
13$num = 42;
14$badStr = new BadStr;
15
16try { $x = $str . $badStr; }
17catch (Exception $e) { echo $e->getMessage(), "\n"; }
18try { $x = $badStr . $str; }
19catch (Exception $e) { echo $e->getMessage(), "\n"; }
20try { $x = $str .= $badStr; }
21catch (Exception $e) { echo $e->getMessage(), "\n"; }
22var_dump($str);
23try { $x = $num . $badStr; }
24catch (Exception $e) { echo $e->getMessage(), "\n"; }
25try { $x = $badStr . $num; }
26catch (Exception $e) { echo $e->getMessage(), "\n"; }
27try { $x = $num .= $badStr; }
28catch (Exception $e) { echo $e->getMessage(), "\n"; }
29var_dump($num);
30
31try { $x = $badStr .= $str; }
32catch (Exception $e) { echo $e->getMessage(), "\n"; }
33var_dump($badStr);
34try { $x = $badStr .= $badStr; }
35catch (Exception $e) { echo $e->getMessage(), "\n"; }
36var_dump($badStr);
37
38try { $x = "x$badStr"; }
39catch (Exception $e) { echo $e->getMessage(), "\n"; }
40try { $x = "{$badStr}x"; }
41catch (Exception $e) { echo $e->getMessage(), "\n"; }
42try { $x = "$str$badStr"; }
43catch (Exception $e) { echo $e->getMessage(), "\n"; }
44try { $x = "$badStr$str"; }
45catch (Exception $e) { echo $e->getMessage(), "\n"; }
46
47try { $x = "x$badStr$str"; }
48catch (Exception $e) { echo $e->getMessage(), "\n"; }
49try { $x = "x$str$badStr"; }
50catch (Exception $e) { echo $e->getMessage(), "\n"; }
51try { $x = "{$str}x$badStr"; }
52catch (Exception $e) { echo $e->getMessage(), "\n"; }
53try { $x = "{$badStr}x$str"; }
54catch (Exception $e) { echo $e->getMessage(), "\n"; }
55
56try { $x = (string) $badStr; }
57catch (Exception $e) { echo $e->getMessage(), "\n"; }
58
59try { $x = include $badStr; }
60catch (Exception $e) { echo $e->getMessage(), "\n"; }
61
62try { echo $badStr; }
63catch (Exception $e) { echo $e->getMessage(), "\n"; }
64
65${""} = 42;
66try { unset(${$badStr}); }
67catch (Exception $e) { echo $e->getMessage(), "\n"; }
68var_dump(${""});
69
70unset(${""});
71try { $x = ${$badStr}; }
72catch (Exception $e) { echo $e->getMessage(), "\n"; }
73
74try { $x = isset(${$badStr}); }
75catch (Exception $e) { echo $e->getMessage(), "\n"; }
76
77$obj = new stdClass;
78try { $x = $obj->{$badStr} = $str; }
79catch (Exception $e) { echo $e->getMessage(), "\n"; }
80var_dump($obj);
81
82try { $str[0] = $badStr; }
83catch (Exception $e) { echo $e->getMessage(), "\n"; }
84var_dump($str);
85
86$obj = new DateInterval('P1D');
87try { $x = $obj->{$badStr} = $str; }
88catch (Exception $e) { echo $e->getMessage(), "\n"; }
89var_dump(!isset($obj->{""}));
90
91try { strlen($badStr); } catch (Exception $e) { echo "Exception\n"; }
92try { substr($badStr, 0); } catch (Exception $e) { echo "Exception\n"; }
93try { new ArrayObject([], 0, $badStr); } catch (Exception $e) { echo "Exception\n"; }
94
95?>
96--EXPECT--
97Exception
98Exception
99Exception
100string(1) "a"
101Exception
102Exception
103Exception
104int(42)
105Exception
106object(BadStr)#1 (0) {
107}
108Exception
109object(BadStr)#1 (0) {
110}
111Exception
112Exception
113Exception
114Exception
115Exception
116Exception
117Exception
118Exception
119Exception
120Exception
121Exception
122Exception
123int(42)
124Exception
125Exception
126Exception
127object(stdClass)#2 (0) {
128}
129Exception
130string(1) "a"
131Exception
132bool(true)
133Exception
134Exception
135Exception
136