tostring_004.phpt 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. --TEST--
  2. Object to string conversion: error cases and behaviour variations.
  3. --FILE--
  4. <?php
  5. function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
  6. echo "Error: $err_no - $err_msg\n";
  7. }
  8. set_error_handler('test_error_handler');
  9. error_reporting(8191);
  10. echo "Object with no __toString():\n";
  11. $obj = new stdClass;
  12. echo "Try 1:\n";
  13. printf($obj);
  14. printf("\n");
  15. echo "\nTry 2:\n";
  16. printf($obj . "\n");
  17. echo "\n\nObject with bad __toString():\n";
  18. class badToString {
  19. function __toString() {
  20. return 0;
  21. }
  22. }
  23. $obj = new badToString;
  24. echo "Try 1:\n";
  25. printf($obj);
  26. printf("\n");
  27. echo "\nTry 2:\n";
  28. printf($obj . "\n");
  29. ?>
  30. --EXPECT--
  31. Object with no __toString():
  32. Try 1:
  33. Error: 4096 - Object of class stdClass could not be converted to string
  34. Object
  35. Try 2:
  36. Error: 4096 - Object of class stdClass could not be converted to string
  37. Object with bad __toString():
  38. Try 1:
  39. Error: 4096 - Method badToString::__toString() must return a string value
  40. Try 2:
  41. Error: 4096 - Method badToString::__toString() must return a string value