countable_count_variation1.phpt 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. --TEST--
  2. SPL: Countable::count() with wrong return types and exception.
  3. --FILE--
  4. <?php
  5. Class returnNull implements Countable {
  6. function count() {
  7. }
  8. }
  9. Class returnString implements Countable {
  10. function count() {
  11. return "hello";
  12. }
  13. }
  14. Class returnObject implements Countable {
  15. function count() {
  16. return new returnObject;
  17. }
  18. }
  19. Class returnArray implements Countable {
  20. function count() {
  21. return array(1,2,3);
  22. }
  23. }
  24. Class throwException implements Countable {
  25. function count() {
  26. throw new Exception('Thrown from count');
  27. }
  28. }
  29. echo "Count returns null:\n";
  30. var_dump(count(new returnNull));
  31. echo "Count returns a string:\n";
  32. var_dump(count(new returnString));
  33. echo "Count returns an object:\n";
  34. var_dump(count(new returnObject));
  35. echo "Count returns an array:\n";
  36. var_dump(count(new returnArray));
  37. echo "Count throws an exception:\n";
  38. try {
  39. echo count(new throwException);
  40. } catch (Exception $e) {
  41. echo $e->getMessage();
  42. }
  43. ?>
  44. --EXPECTF--
  45. Count returns null:
  46. int(0)
  47. Count returns a string:
  48. int(0)
  49. Count returns an object:
  50. Notice: Object of class returnObject could not be converted to int in %s on line 40
  51. int(1)
  52. Count returns an array:
  53. int(1)
  54. Count throws an exception:
  55. Thrown from count