tostring_001.phpt 1.9 KB

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