002.phpt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. --TEST--
  2. Test throw with various expressions
  3. --FILE--
  4. <?php
  5. class Foo {
  6. public function createNotFoundException() {
  7. return new Exception('Not found');
  8. }
  9. public function throwException() {
  10. throw $this->createNotFoundException();
  11. }
  12. public static function staticCreateNotFoundException() {
  13. return new Exception('Static not found');
  14. }
  15. public static function staticThrowException() {
  16. throw static::staticCreateNotFoundException();
  17. }
  18. }
  19. try {
  20. (new Foo())->throwException();
  21. } catch(Exception $e) {
  22. echo $e->getMessage() . "\n";
  23. }
  24. try {
  25. Foo::staticThrowException();
  26. } catch(Exception $e) {
  27. echo $e->getMessage() . "\n";
  28. }
  29. try {
  30. throw true ? new Exception('Ternary true 1') : new Exception('Ternary true 2');
  31. } catch(Exception $e) {
  32. echo $e->getMessage() . "\n";
  33. }
  34. try {
  35. throw false ? new Exception('Ternary false 1') : new Exception('Ternary false 2');
  36. } catch(Exception $e) {
  37. echo $e->getMessage() . "\n";
  38. }
  39. try {
  40. $exception1 = new Exception('Coalesce non-null 1');
  41. $exception2 = new Exception('Coalesce non-null 2');
  42. throw $exception1 ?? $exception2;
  43. } catch(Exception $e) {
  44. echo $e->getMessage() . "\n";
  45. }
  46. try {
  47. $exception1 = null;
  48. $exception2 = new Exception('Coalesce null 2');
  49. throw $exception1 ?? $exception2;
  50. } catch(Exception $e) {
  51. echo $e->getMessage() . "\n";
  52. }
  53. try {
  54. throw $exception = new Exception('Assignment');
  55. } catch(Exception $e) {
  56. echo $e->getMessage() . "\n";
  57. }
  58. try {
  59. $exception = null;
  60. throw $exception ??= new Exception('Coalesce assignment null');
  61. } catch(Exception $e) {
  62. echo $e->getMessage() . "\n";
  63. }
  64. try {
  65. $exception = new Exception('Coalesce assignment non-null 1');
  66. throw $exception ??= new Exception('Coalesce assignment non-null 2');
  67. } catch(Exception $e) {
  68. echo $e->getMessage() . "\n";
  69. }
  70. $andConditionalTest = function ($condition1, $condition2) {
  71. throw $condition1 && $condition2
  72. ? new Exception('And in conditional 1')
  73. : new Exception('And in conditional 2');
  74. };
  75. try {
  76. $andConditionalTest(false, false);
  77. } catch(Exception $e) {
  78. echo $e->getMessage() . "\n";
  79. }
  80. try {
  81. $andConditionalTest(false, true);
  82. } catch (Exception $e) {
  83. echo $e->getMessage() . "\n";
  84. }
  85. try {
  86. $andConditionalTest(true, false);
  87. } catch (Exception $e) {
  88. echo $e->getMessage() . "\n";
  89. }
  90. try {
  91. $andConditionalTest(true, true);
  92. } catch (Exception $e) {
  93. echo $e->getMessage() . "\n";
  94. }
  95. ?>
  96. --EXPECT--
  97. Not found
  98. Static not found
  99. Ternary true 1
  100. Ternary false 2
  101. Coalesce non-null 1
  102. Coalesce null 2
  103. Assignment
  104. Coalesce assignment null
  105. Coalesce assignment non-null 1
  106. And in conditional 2
  107. And in conditional 2
  108. And in conditional 2
  109. And in conditional 1