php-src/tests/classes/tostring_001.phpt
George Peter Banyard 99fa740acb
Use common function for TypeError on illegal offset access (#10544)
This merges all usages of emitting an offset TypeError into a new ZEND_API function
zend_illegal_container_offset(const zend_string* container, const zval *offset, int type);

Where the container should represent the type on which the access is attempted (e.g. string, array)
The offset zval that is used, where the error message will display its type
The type of access, which should be a BP_VAR_* constant, to get special message for isset/empty/unset
2023-06-06 11:28:19 +01:00

135 lines
2 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()
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====