php-src/tests/classes/tostring_001.phpt
Máté Kocsis fbe30592d6
Improve type error messages when an object is given
From now on, we always display the given object's type instead of just reporting "object".
Additionally, make the format of return type errors match the format of argument errors.

Closes GH-5625
2020-05-26 19:06:19 +02:00

136 lines
1.9 KiB
PHP

--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()
Illegal offset type
====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====