xref: /PHP-7.1/tests/classes/tostring_004.phpt (revision 113213f0)
1--TEST--
2Object to string conversion: error cases and behaviour variations.
3--FILE--
4<?php
5function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
6        echo "Error: $err_no - $err_msg\n";
7}
8set_error_handler('test_error_handler');
9error_reporting(8191);
10
11
12echo "Object with no __toString():\n";
13$obj = new stdClass;
14echo "Try 1:\n";
15printf($obj);
16printf("\n");
17
18echo "\nTry 2:\n";
19printf($obj . "\n");
20
21
22echo "\n\nObject with bad __toString():\n";
23class badToString {
24	function __toString() {
25		return 0;
26	}
27}
28$obj = new badToString;
29echo "Try 1:\n";
30printf($obj);
31printf("\n");
32
33echo "\nTry 2:\n";
34printf($obj . "\n");
35
36?>
37--EXPECTF--
38Object with no __toString():
39Try 1:
40Error: 4096 - Object of class stdClass could not be converted to string
41Object
42
43Try 2:
44Error: 4096 - Object of class stdClass could not be converted to string
45
46
47
48Object with bad __toString():
49Try 1:
50Error: 4096 - Method badToString::__toString() must return a string value
51
52
53Try 2:
54Error: 4096 - Method badToString::__toString() must return a string value
55