exception_from_toString.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. --TEST--
  2. Test exceptions thrown from __toString() in various contexts
  3. --FILE--
  4. <?php
  5. class BadStr {
  6. public function __toString() {
  7. throw new Exception("Exception");
  8. }
  9. }
  10. $str = "a";
  11. $num = 42;
  12. $badStr = new BadStr;
  13. try { $x = $str . $badStr; }
  14. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  15. try { $x = $badStr . $str; }
  16. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  17. try { $x = $str .= $badStr; }
  18. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  19. var_dump($str);
  20. try { $x = $num . $badStr; }
  21. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  22. try { $x = $badStr . $num; }
  23. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  24. try { $x = $num .= $badStr; }
  25. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  26. var_dump($num);
  27. try { $x = $badStr .= $str; }
  28. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  29. var_dump($badStr);
  30. try { $x = $badStr .= $badStr; }
  31. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  32. var_dump($badStr);
  33. try { $x = "x$badStr"; }
  34. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  35. try { $x = "{$badStr}x"; }
  36. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  37. try { $x = "$str$badStr"; }
  38. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  39. try { $x = "$badStr$str"; }
  40. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  41. try { $x = "x$badStr$str"; }
  42. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  43. try { $x = "x$str$badStr"; }
  44. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  45. try { $x = "{$str}x$badStr"; }
  46. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  47. try { $x = "{$badStr}x$str"; }
  48. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  49. try { $x = (string) $badStr; }
  50. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  51. try { $x = include $badStr; }
  52. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  53. try { echo $badStr; }
  54. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  55. ${""} = 42;
  56. try { unset(${$badStr}); }
  57. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  58. var_dump(${""});
  59. unset(${""});
  60. try { $x = ${$badStr}; }
  61. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  62. try { $x = isset(${$badStr}); }
  63. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  64. $obj = new stdClass;
  65. try { $x = $obj->{$badStr} = $str; }
  66. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  67. var_dump($obj);
  68. try { $str[0] = $badStr; }
  69. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  70. var_dump($str);
  71. $obj = new DateInterval('P1D');
  72. try { $x = $obj->{$badStr} = $str; }
  73. catch (Exception $e) { echo $e->getMessage(), "\n"; }
  74. var_dump(!isset($obj->{""}));
  75. try { strlen($badStr); } catch (Exception $e) { echo "Exception\n"; }
  76. try { substr($badStr, 0); } catch (Exception $e) { echo "Exception\n"; }
  77. try { new ArrayObject([], 0, $badStr); } catch (Exception $e) { echo "Exception\n"; }
  78. ?>
  79. --EXPECT--
  80. Exception
  81. Exception
  82. Exception
  83. string(1) "a"
  84. Exception
  85. Exception
  86. Exception
  87. int(42)
  88. Exception
  89. object(BadStr)#1 (0) {
  90. }
  91. Exception
  92. object(BadStr)#1 (0) {
  93. }
  94. Exception
  95. Exception
  96. Exception
  97. Exception
  98. Exception
  99. Exception
  100. Exception
  101. Exception
  102. Exception
  103. Exception
  104. Exception
  105. Exception
  106. int(42)
  107. Exception
  108. Exception
  109. Exception
  110. object(stdClass)#2 (0) {
  111. }
  112. Exception
  113. string(1) "a"
  114. Exception
  115. bool(true)
  116. Exception
  117. Exception
  118. Exception