new.phpt 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. --TEST--
  2. new in constant expressions
  3. --FILE--
  4. <?php
  5. try {
  6. eval('static $a = new DoesNotExist;');
  7. } catch (Error $e) {
  8. echo $e->getMessage(), "\n";
  9. }
  10. static $b = new stdClass;
  11. var_dump($b);
  12. try {
  13. eval('static $c = new stdClass([] + 0);');
  14. } catch (Error $e) {
  15. echo $e->getMessage(), "\n";
  16. }
  17. class Test {
  18. public function __construct(public $a, public $b) {}
  19. }
  20. try {
  21. eval('static $d = new Test(new stdClass, [] + 0);');
  22. } catch (Error $e) {
  23. echo $e->getMessage(), "\n";
  24. }
  25. static $e = new Test(new stdClass, 42);
  26. var_dump($e);
  27. class Test2 {
  28. public function __construct() {
  29. echo "Side-effect\n";
  30. throw new Exception("Failed to construct");
  31. }
  32. }
  33. try {
  34. eval('static $f = new Test2();');
  35. } catch (Exception $e) {
  36. echo $e->getMessage(), "\n";
  37. }
  38. ?>
  39. --EXPECT--
  40. Class "DoesNotExist" not found
  41. object(stdClass)#2 (0) {
  42. }
  43. Unsupported operand types: array + int
  44. Unsupported operand types: array + int
  45. object(Test)#4 (2) {
  46. ["a"]=>
  47. object(stdClass)#1 (0) {
  48. }
  49. ["b"]=>
  50. int(42)
  51. }
  52. Side-effect
  53. Failed to construct