123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- --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====
|