xref: /PHP-5.6/tests/classes/tostring_001.phpt (revision 610c7fbe)
1--TEST--
2ZE2 __toString()
3--FILE--
4<?php
5
6function my_error_handler($errno, $errstr, $errfile, $errline) {
7	var_dump($errstr);
8}
9
10set_error_handler('my_error_handler');
11
12class test1
13{
14}
15
16class test2
17{
18    function __toString()
19    {
20    	echo __METHOD__ . "()\n";
21        return "Converted\n";
22    }
23}
24
25class test3
26{
27    function __toString()
28    {
29    	echo __METHOD__ . "()\n";
30        return 42;
31    }
32}
33echo "====test1====\n";
34$o = new test1;
35print_r($o);
36var_dump((string)$o);
37var_dump($o);
38
39echo "====test2====\n";
40$o = new test2;
41print_r($o);
42print $o;
43var_dump($o);
44echo "====test3====\n";
45echo $o;
46
47echo "====test4====\n";
48echo "string:".$o;
49
50echo "====test5====\n";
51echo 1 . $o;
52echo 1 , $o;
53
54echo "====test6====\n";
55echo $o . $o;
56echo $o , $o;
57
58echo "====test7====\n";
59$ar = array();
60$ar[$o->__toString()] = "ERROR";
61echo $ar[$o];
62
63echo "====test8====\n";
64var_dump(trim($o));
65var_dump(trim((string)$o));
66
67echo "====test9====\n";
68echo sprintf("%s", $o);
69
70echo "====test10====\n";
71$o = new test3;
72var_dump($o);
73echo $o;
74
75?>
76====DONE====
77--EXPECTF--
78====test1====
79test1 Object
80(
81)
82string(54) "Object of class test1 could not be converted to string"
83string(0) ""
84object(test1)#%d (0) {
85}
86====test2====
87test2 Object
88(
89)
90test2::__toString()
91Converted
92object(test2)#%d (0) {
93}
94====test3====
95test2::__toString()
96Converted
97====test4====
98test2::__toString()
99string:Converted
100====test5====
101test2::__toString()
1021Converted
1031test2::__toString()
104Converted
105====test6====
106test2::__toString()
107test2::__toString()
108Converted
109Converted
110test2::__toString()
111Converted
112test2::__toString()
113Converted
114====test7====
115test2::__toString()
116string(19) "Illegal offset type"
117====test8====
118test2::__toString()
119string(9) "Converted"
120test2::__toString()
121string(9) "Converted"
122====test9====
123test2::__toString()
124Converted
125====test10====
126object(test3)#%d (0) {
127}
128test3::__toString()
129string(53) "Method test3::__toString() must return a string value"
130====DONE====
131