1--TEST--
2Zend: Test object_init_with_constructor() API
3--EXTENSIONS--
4zend_test
5sysvmsg
6--FILE--
7<?php
8
9class PrivateUser {
10    private function __construct() {
11        return new stdClass();
12    }
13    public function __destruct() {
14        echo 'Destructor for ', __CLASS__, PHP_EOL;
15    }
16}
17
18class ThrowingUser {
19    public function __construct() {
20        throw new Exception("Don't construct");
21    }
22    public function __destruct() {
23        echo 'Destructor for ', __CLASS__, PHP_EOL;
24    }
25}
26
27abstract class AbstractClass {
28    public function __construct() {
29        return new stdClass();
30    }
31    public function __destruct() {
32        echo 'Destructor for ', __CLASS__, PHP_EOL;
33    }
34}
35
36class TestUserWithConstructorArgs {
37    public function __construct(int $int_param, string $string_param) {
38        return new stdClass();
39    }
40    public function __destruct() {
41        echo 'Destructor for ', __CLASS__, PHP_EOL;
42    }
43}
44
45class TestUserWithConstructorNoParams {
46    public function __construct() {
47        return new stdClass();
48    }
49    public function __destruct() {
50        echo 'Destructor for ', __CLASS__, PHP_EOL;
51    }
52}
53
54echo "Testing impossible initializations\n";
55try {
56    $o = zend_object_init_with_constructor("_ZendTestInterface");
57    var_dump($o);
58    unset($o);
59} catch (\Throwable $e) {
60    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
61}
62try {
63    $o = zend_object_init_with_constructor("_ZendTestTrait");
64    var_dump($o);
65    unset($o);
66} catch (\Throwable $e) {
67    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
68}
69try {
70    $o = zend_object_init_with_constructor("ZendTestUnitEnum");
71    var_dump($o);
72    unset($o);
73} catch (\Throwable $e) {
74    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
75}
76try {
77    $o = zend_object_init_with_constructor("AbstractClass");
78    var_dump($o);
79    unset($o);
80} catch (\Throwable $e) {
81    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
82}
83try {
84    $o = zend_object_init_with_constructor("SysvMessageQueue");
85    var_dump($o);
86    unset($o);
87} catch (\Throwable $e) {
88    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
89}
90try {
91    $o = zend_object_init_with_constructor("PrivateUser");
92    var_dump($o);
93    unset($o);
94} catch (\Throwable $e) {
95    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
96}
97try {
98    $o = zend_object_init_with_constructor("ThrowingUser");
99    var_dump($o);
100    unset($o);
101} catch (\Throwable $e) {
102    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
103}
104
105echo "Testing param passing\n";
106try {
107    $o = zend_object_init_with_constructor("TestUserWithConstructorArgs");
108    var_dump($o);
109    unset($o);
110} catch (\Throwable $e) {
111    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
112}
113try {
114    $o = zend_object_init_with_constructor("TestUserWithConstructorArgs", "str", 5);
115    var_dump($o);
116    unset($o);
117} catch (\Throwable $e) {
118    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
119}
120try {
121    $o = zend_object_init_with_constructor("TestUserWithConstructorArgs", 5, string_param: "str", unused_param: 15.3);
122    var_dump($o);
123    unset($o);
124} catch (\Throwable $e) {
125    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
126}
127
128$o = zend_object_init_with_constructor("TestUserWithConstructorArgs", 5, string_param: "str");
129var_dump($o);
130unset($o);
131
132echo "Passing too many args to constructor\n";
133$o = zend_object_init_with_constructor("TestUserWithConstructorArgs", 5, "str", 'unused_param');
134var_dump($o);
135unset($o);
136
137echo "Testing class with defined constructor and no params\n";
138$o = zend_object_init_with_constructor("TestUserWithConstructorNoParams");
139var_dump($o);
140unset($o);
141?>
142--EXPECT--
143Testing impossible initializations
144Error: Cannot instantiate interface _ZendTestInterface
145Error: Cannot instantiate trait _ZendTestTrait
146Error: Cannot instantiate enum ZendTestUnitEnum
147Error: Cannot instantiate abstract class AbstractClass
148Error: Cannot directly construct SysvMessageQueue, use msg_get_queue() instead
149Error: Call to private PrivateUser::__construct() from global scope
150Exception: Don't construct
151Testing param passing
152ArgumentCountError: Too few arguments to function TestUserWithConstructorArgs::__construct(), 0 passed and exactly 2 expected
153TypeError: TestUserWithConstructorArgs::__construct(): Argument #1 ($int_param) must be of type int, string given
154Error: Unknown named parameter $unused_param
155object(TestUserWithConstructorArgs)#1 (0) {
156}
157Destructor for TestUserWithConstructorArgs
158Passing too many args to constructor
159object(TestUserWithConstructorArgs)#1 (0) {
160}
161Destructor for TestUserWithConstructorArgs
162Testing class with defined constructor and no params
163object(TestUserWithConstructorNoParams)#1 (0) {
164}
165Destructor for TestUserWithConstructorNoParams
166