countable_count_variation1.phpt 1.4 KB

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