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