tostring_004.phpt 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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) {
  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. try {
  14. printf($obj);
  15. } catch (Error $e) {
  16. echo $e->getMessage(), "\n";
  17. }
  18. printf("\n");
  19. echo "\nTry 2:\n";
  20. try {
  21. printf($obj . "\n");
  22. } catch (Error $e) {
  23. echo $e->getMessage(), "\n";
  24. }
  25. echo "\n\nObject with bad __toString():\n";
  26. class badToString {
  27. function __toString() {
  28. return [];
  29. }
  30. }
  31. $obj = new badToString;
  32. echo "Try 1:\n";
  33. try {
  34. printf($obj);
  35. } catch (Error $e) {
  36. echo $e->getMessage(), "\n";
  37. }
  38. printf("\n");
  39. echo "\nTry 2:\n";
  40. try {
  41. printf($obj . "\n");
  42. } catch (Error $e) {
  43. echo $e->getMessage(), "\n";
  44. }
  45. ?>
  46. --EXPECT--
  47. Object with no __toString():
  48. Try 1:
  49. printf(): Argument #1 ($format) must be of type string, stdClass given
  50. Try 2:
  51. Object of class stdClass could not be converted to string
  52. Object with bad __toString():
  53. Try 1:
  54. badToString::__toString(): Return value must be of type string, array returned
  55. Try 2:
  56. badToString::__toString(): Return value must be of type string, array returned