tostring_004.phpt 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. --EXPECTF--
  31. Object with no __toString():
  32. Try 1:
  33. Error: 4096 - Object of class stdClass could not be converted to string
  34. Error: 8 - Object of class stdClass to string conversion
  35. Object
  36. Try 2:
  37. Error: 4096 - Object of class stdClass could not be converted to string
  38. Object with bad __toString():
  39. Try 1:
  40. Error: 4096 - Method badToString::__toString() must return a string value
  41. Try 2:
  42. Error: 4096 - Method badToString::__toString() must return a string value