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