xref: /PHP-5.6/tests/classes/tostring_004.phpt (revision ee0f7bc0)
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
41Error: 8 - Object of class stdClass to string conversion
42Object
43
44Try 2:
45Error: 4096 - Object of class stdClass could not be converted to string
46
47
48
49Object with bad __toString():
50Try 1:
51Error: 4096 - Method badToString::__toString() must return a string value
52
53
54Try 2:
55Error: 4096 - Method badToString::__toString() must return a string value
56
57