xref: /PHP-7.0/Zend/tests/bug26166.phpt (revision 9397f527)
1--TEST--
2Bug #26166 (__toString() crash when no values returned)
3--FILE--
4<?php
5
6class Foo
7{
8    function __toString()
9    {
10        return "Hello World!\n";
11    }
12}
13
14class Bar
15{
16    private $obj;
17
18    function __construct()
19    {
20        $this->obj = new Foo();
21    }
22
23    function __toString()
24    {
25        return $this->obj->__toString();
26    }
27}
28
29$o = new Bar;
30echo $o;
31
32echo "===NONE===\n";
33
34function my_error_handler($errno, $errstr, $errfile, $errline) {
35	var_dump($errstr);
36}
37
38set_error_handler('my_error_handler');
39
40class NoneTest
41{
42	function __toString() {
43	}
44}
45
46$o = new NoneTest;
47echo $o;
48
49echo "===THROW===\n";
50
51class ErrorTest
52{
53	function __toString() {
54		throw new Exception("This is an error!");
55	}
56}
57
58$o = new ErrorTest;
59try {
60	echo $o;
61}
62catch (Exception $e) {
63	echo "Got the exception\n";
64}
65
66?>
67===DONE===
68--EXPECTF--
69Hello World!
70===NONE===
71string(%d) "Method NoneTest::__toString() must return a string value"
72===THROW===
73
74Fatal error: Method ErrorTest::__toString() must not throw an exception, caught Exception: This is an error! in %sbug26166.php on line %d
75