tostring_001.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. --TEST--
  2. ZE2 __toString()
  3. --FILE--
  4. <?php
  5. function my_error_handler($errno, $errstr, $errfile, $errline) {
  6. var_dump($errstr);
  7. }
  8. set_error_handler('my_error_handler');
  9. class test1
  10. {
  11. }
  12. class test2
  13. {
  14. function __toString()
  15. {
  16. echo __METHOD__ . "()\n";
  17. return "Converted\n";
  18. }
  19. }
  20. class test3
  21. {
  22. function __toString()
  23. {
  24. echo __METHOD__ . "()\n";
  25. return 42;
  26. }
  27. }
  28. echo "====test1====\n";
  29. $o = new test1;
  30. print_r($o);
  31. var_dump((string)$o);
  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. echo $ar[$o];
  52. echo "====test8====\n";
  53. var_dump(trim($o));
  54. var_dump(trim((string)$o));
  55. echo "====test9====\n";
  56. echo sprintf("%s", $o);
  57. echo "====test10====\n";
  58. $o = new test3;
  59. var_dump($o);
  60. echo $o;
  61. ?>
  62. ====DONE====
  63. --EXPECTF--
  64. ====test1====
  65. test1 Object
  66. (
  67. )
  68. string(54) "Object of class test1 could not be converted to string"
  69. string(0) ""
  70. object(test1)#%d (0) {
  71. }
  72. ====test2====
  73. test2 Object
  74. (
  75. )
  76. test2::__toString()
  77. Converted
  78. object(test2)#%d (0) {
  79. }
  80. ====test3====
  81. test2::__toString()
  82. Converted
  83. ====test4====
  84. test2::__toString()
  85. string:Converted
  86. ====test5====
  87. test2::__toString()
  88. 1Converted
  89. 1test2::__toString()
  90. Converted
  91. ====test6====
  92. test2::__toString()
  93. test2::__toString()
  94. Converted
  95. Converted
  96. test2::__toString()
  97. Converted
  98. test2::__toString()
  99. Converted
  100. ====test7====
  101. test2::__toString()
  102. string(19) "Illegal offset type"
  103. ====test8====
  104. test2::__toString()
  105. string(9) "Converted"
  106. test2::__toString()
  107. string(9) "Converted"
  108. ====test9====
  109. test2::__toString()
  110. Converted
  111. ====test10====
  112. object(test3)#%d (0) {
  113. }
  114. test3::__toString()
  115. string(53) "Method test3::__toString() must return a string value"
  116. ====DONE====