Skip to content

Commit fda5cc2

Browse files
committed
Merge branch 'PHP-8.6'
* PHP-8.6: Zend: fix object_init_with_constructor() with positional args in named_params zend_test: add additional tests with zend_object_init_with_constructor() zend_test: add missing opcache enable INI setting
2 parents 5859090 + 2581b6e commit fda5cc2

8 files changed

Lines changed: 313 additions & 18 deletions

‎Zend/zend_API.c‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,19 +1943,21 @@ ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *c
19431943
* However, if there are named arguments (and it is not empty),
19441944
* an Error must be thrown to be consistent with new ClassName() */
19451945
if (UNEXPECTED(named_params != NULL && zend_hash_num_elements(named_params) != 0)) {
1946-
/* Throw standard Error */
1947-
zend_string *arg_name = NULL;
1948-
zend_hash_get_current_key(named_params, &arg_name, /* num_index */ NULL);
1949-
ZEND_ASSERT(arg_name != NULL);
1950-
zend_throw_error(NULL, "Unknown named parameter $%pS", arg_name);
1951-
/* Do not call destructor, free object, and set arg to IS_UNDEF */
1952-
zend_object_store_ctor_failed(obj);
1953-
zval_ptr_dtor(arg);
1954-
ZVAL_UNDEF(arg);
1955-
return FAILURE;
1956-
} else {
1957-
return SUCCESS;
1946+
/* As named_arguments may contain positional args we need to check there is actually a string key */
1947+
ZEND_HASH_FOREACH_STR_KEY(named_params, zend_string *arg_name) {
1948+
if (arg_name == NULL) {
1949+
continue;
1950+
}
1951+
/* Throw standard Error */
1952+
zend_throw_error(NULL, "Unknown named parameter $%pS", arg_name);
1953+
/* Do not call destructor, free object, and set arg to IS_UNDEF */
1954+
zend_object_store_ctor_failed(obj);
1955+
zval_ptr_dtor(arg);
1956+
ZVAL_UNDEF(arg);
1957+
return FAILURE;
1958+
} ZEND_HASH_FOREACH_END();
19581959
}
1960+
return SUCCESS;
19591961
}
19601962
/* A constructor should not return a value, however if an exception is thrown
19611963
* zend_call_known_function() will set the retval to IS_UNDEF */

‎ext/zend_test/test.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,27 @@ static ZEND_FUNCTION(zend_object_init_with_constructor)
11621162
ZVAL_COPY_VALUE(return_value, &obj);
11631163
}
11641164

1165+
/* Instantiate a class and run the constructor via object_init_with_constructor */
1166+
static ZEND_FUNCTION(zend_object_init_with_constructor_array)
1167+
{
1168+
zend_class_entry *ce = NULL;
1169+
HashTable *args;
1170+
1171+
ZEND_PARSE_PARAMETERS_START(2, 2)
1172+
Z_PARAM_CLASS(ce)
1173+
Z_PARAM_ARRAY_HT(args)
1174+
ZEND_PARSE_PARAMETERS_END();
1175+
1176+
zval obj;
1177+
/* We don't use return_value directly to check for memory leaks of the API on failure */
1178+
zend_result status = object_init_with_constructor(&obj, ce, 0, NULL, args);
1179+
if (status == FAILURE) {
1180+
RETURN_THROWS();
1181+
}
1182+
ZEND_ASSERT(!EG(exception));
1183+
ZVAL_COPY_VALUE(return_value, &obj);
1184+
}
1185+
11651186
static ZEND_FUNCTION(zend_call_method_if_exists)
11661187
{
11671188
zend_object *obj = NULL;

‎ext/zend_test/test.stub.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ function zend_get_current_func_name(): string {}
411411
function zend_call_method(object|string $obj_or_class, string $method, mixed $arg1 = UNKNOWN, mixed $arg2 = UNKNOWN): mixed {}
412412

413413
function zend_object_init_with_constructor(string $class, mixed ...$args): mixed {}
414+
function zend_object_init_with_constructor_array(string $class, array $args): mixed {}
414415

415416
function zend_call_method_if_exists(object $obj, string $method, mixed ...$args): mixed {}
416417

‎ext/zend_test/test_arginfo.h‎

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ext/zend_test/test_decl.h‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ext/zend_test/test_legacy_arginfo.h‎

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
--TEST--
2+
Zend: Test object_init_with_constructor() API
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
class TestUserWithConstructorArgs {
9+
public function __construct(int $int_param, string $string_param) {
10+
}
11+
public function __destruct() {
12+
echo 'Destructor for ', __CLASS__, PHP_EOL;
13+
}
14+
}
15+
16+
class TestUserWithConstructorNoParams {
17+
public function __construct() {
18+
}
19+
public function __destruct() {
20+
echo 'Destructor for ', __CLASS__, PHP_EOL;
21+
}
22+
}
23+
24+
class TestUserWithoutConstructor {
25+
public function __destruct() {
26+
echo 'Destructor for ', __CLASS__, PHP_EOL;
27+
}
28+
}
29+
30+
$args = [5, 'str'];
31+
$argsNamed = [5, 'string_param' => 'str'];
32+
$argsNamedExtraUnknown = [5, 'string_param' => 'str', 'unknown' => 'unknown'];
33+
34+
echo "Testing param passing\n";
35+
echo "Test TestUserWithConstructorArgs:\n";
36+
$o = zend_object_init_with_constructor_array("TestUserWithConstructorArgs", $args);
37+
var_dump($o);
38+
unset($o);
39+
40+
$o = zend_object_init_with_constructor_array("TestUserWithConstructorArgs", $argsNamed);
41+
var_dump($o);
42+
unset($o);
43+
44+
try {
45+
$o = zend_object_init_with_constructor_array("TestUserWithConstructorArgs", $argsNamedExtraUnknown);
46+
var_dump($o);
47+
unset($o);
48+
} catch (\Throwable $e) {
49+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
50+
}
51+
52+
echo "Test TestUserWithConstructorNoParams:\n";
53+
try {
54+
$o = zend_object_init_with_constructor_array("TestUserWithConstructorNoParams", $args);
55+
var_dump($o);
56+
unset($o);
57+
} catch (\Throwable $e) {
58+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
59+
}
60+
61+
try {
62+
$o = zend_object_init_with_constructor_array("TestUserWithConstructorNoParams", $argsNamed);
63+
var_dump($o);
64+
unset($o);
65+
} catch (\Throwable $e) {
66+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
67+
}
68+
69+
try {
70+
$o = zend_object_init_with_constructor_array("TestUserWithConstructorNoParams", $argsNamedExtraUnknown);
71+
var_dump($o);
72+
unset($o);
73+
} catch (\Throwable $e) {
74+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
75+
}
76+
77+
echo "Test TestUserWithoutConstructor:\n";
78+
$o = zend_object_init_with_constructor_array("TestUserWithoutConstructor", $args);
79+
var_dump($o);
80+
unset($o);
81+
82+
try {
83+
$o = zend_object_init_with_constructor_array("TestUserWithoutConstructor", $argsNamed);
84+
var_dump($o);
85+
unset($o);
86+
} catch (\Throwable $e) {
87+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
88+
}
89+
90+
try {
91+
$o = zend_object_init_with_constructor_array("TestUserWithoutConstructor", $argsNamedExtraUnknown);
92+
var_dump($o);
93+
unset($o);
94+
} catch (\Throwable $e) {
95+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
96+
}
97+
98+
echo "Test _ZendTestMagicCall (internal class no ctor):\n";
99+
try {
100+
$o = zend_object_init_with_constructor_array("_ZendTestMagicCall", $args);
101+
var_dump($o);
102+
unset($o);
103+
} catch (\Throwable $e) {
104+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
105+
}
106+
107+
try {
108+
$o = zend_object_init_with_constructor_array("_ZendTestMagicCall", $argsNamed);
109+
var_dump($o);
110+
unset($o);
111+
} catch (\Throwable $e) {
112+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
113+
}
114+
115+
try {
116+
$o = zend_object_init_with_constructor_array("_ZendTestMagicCall", $argsNamedExtraUnknown);
117+
var_dump($o);
118+
unset($o);
119+
} catch (\Throwable $e) {
120+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
121+
}
122+
123+
?>
124+
--EXPECT--
125+
Testing param passing
126+
Test TestUserWithConstructorArgs:
127+
object(TestUserWithConstructorArgs)#1 (0) {
128+
}
129+
Destructor for TestUserWithConstructorArgs
130+
object(TestUserWithConstructorArgs)#1 (0) {
131+
}
132+
Destructor for TestUserWithConstructorArgs
133+
Error: Unknown named parameter $unknown
134+
Test TestUserWithConstructorNoParams:
135+
object(TestUserWithConstructorNoParams)#1 (0) {
136+
}
137+
Destructor for TestUserWithConstructorNoParams
138+
Error: Unknown named parameter $string_param
139+
Error: Unknown named parameter $string_param
140+
Test TestUserWithoutConstructor:
141+
object(TestUserWithoutConstructor)#3 (0) {
142+
}
143+
Destructor for TestUserWithoutConstructor
144+
Error: Unknown named parameter $string_param
145+
Error: Unknown named parameter $string_param
146+
Test _ZendTestMagicCall (internal class no ctor):
147+
object(_ZendTestMagicCall)#2 (0) {
148+
}
149+
Error: Unknown named parameter $string_param
150+
Error: Unknown named parameter $string_param

0 commit comments

Comments
 (0)