[php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5

From: Date: Sun, 08 Nov 2026 07:45:28 +0000
Subject: [php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143416@lists.php.net to get a copy of this message
Author: Gina Peter Banyard (Girgias) Date: 2026-11-08T07:38:27Z Commit: https://github.com/php/php-src/commit/1e24c5187863c31244ff6544bce63c894c5b56b1 Raw diff: https://github.com/php/php-src/commit/1e24c5187863c31244ff6544bce63c894c5b56b1.diff Merge branch 'PHP-8.4' into PHP-8.5 * PHP-8.4: 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 Changed paths: A ext/zend_test/tests/zend_object_init_with_constructor_array.phpt M Zend/zend_API.c M ext/zend_test/test.c M ext/zend_test/test.stub.php M ext/zend_test/test_arginfo.h M ext/zend_test/tests/zend_object_init_with_constructor_classes_without_constructor.phpt Diff: diff --git a/Zend/zend_API.c b/Zend/zend_API.c index 1bd7e64de5f5..d196ff2061af 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -1904,19 +1904,21 @@ ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *c * However, if there are named arguments (and it is not empty), * an Error must be thrown to be consistent with new ClassName() */ if (UNEXPECTED(named_params != NULL && zend_hash_num_elements(named_params) != 0)) { - /* Throw standard Error */ - zend_string *arg_name = NULL; - zend_hash_get_current_key(named_params, &arg_name, /* num_index */ NULL); - ZEND_ASSERT(arg_name != NULL); - zend_throw_error(NULL, "Unknown named parameter $%s", ZSTR_VAL(arg_name)); - /* Do not call destructor, free object, and set arg to IS_UNDEF */ - zend_object_store_ctor_failed(obj); - zval_ptr_dtor(arg); - ZVAL_UNDEF(arg); - return FAILURE; - } else { - return SUCCESS; + /* As named_arguments may contain positional args we need to check there is actually a string key */ + ZEND_HASH_FOREACH_STR_KEY(named_params, zend_string *arg_name) { + if (arg_name == NULL) { + continue; + } + /* Throw standard Error */ + zend_throw_error(NULL, "Unknown named parameter $%s", ZSTR_VAL(arg_name)); + /* Do not call destructor, free object, and set arg to IS_UNDEF */ + zend_object_store_ctor_failed(obj); + zval_ptr_dtor(arg); + ZVAL_UNDEF(arg); + return FAILURE; + } ZEND_HASH_FOREACH_END(); } + return SUCCESS; } /* A constructor should not return a value, however if an exception is thrown * zend_call_known_function() will set the retval to IS_UNDEF */ diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index b67815f11ed9..3831759c4a49 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -600,6 +600,27 @@ static ZEND_FUNCTION(zend_object_init_with_constructor) ZVAL_COPY_VALUE(return_value, &obj); } +/* Instantiate a class and run the constructor via object_init_with_constructor */ +static ZEND_FUNCTION(zend_object_init_with_constructor_array) +{ + zend_class_entry *ce = NULL; + HashTable *args; + + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_CLASS(ce) + Z_PARAM_ARRAY_HT(args) + ZEND_PARSE_PARAMETERS_END(); + + zval obj; + /* We don't use return_value directly to check for memory leaks of the API on failure */ + zend_result status = object_init_with_constructor(&obj, ce, 0, NULL, args); + if (status == FAILURE) { + RETURN_THROWS(); + } + ZEND_ASSERT(!EG(exception)); + ZVAL_COPY_VALUE(return_value, &obj); +} + static ZEND_FUNCTION(zend_call_method_if_exists) { zend_object *obj = NULL; diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index e6a8eee79cf2..500240d0f24e 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -305,6 +305,7 @@ function zend_get_current_func_name(): string {} function zend_call_method(object|string $obj_or_class, string $method, mixed $arg1 = UNKNOWN, mixed $arg2 = UNKNOWN): mixed {} function zend_object_init_with_constructor(string $class, mixed ...$args): mixed {} + function zend_object_init_with_constructor_array(string $class, array $args): mixed {} function zend_call_method_if_exists(object $obj, string $method, mixed ...$args): mixed {} diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index d35ae490d8d8..40c38d2e65c5 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 5e786debe5cdc90081351dd71ac16bd64a3cf1a6 */ + * Stub hash: a37d99a2ffb58e76d93e3fd260f33fe2c0c4c10e */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_trigger_bailout, 0, 0, IS_NEVER, 0) ZEND_END_ARG_INFO() @@ -129,6 +129,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_object_init_with_constructo ZEND_ARG_VARIADIC_TYPE_INFO(0, args, IS_MIXED, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_object_init_with_constructor_array, 0, 2, IS_MIXED, 0) + ZEND_ARG_TYPE_INFO(0, class, IS_STRING, 0) + ZEND_ARG_TYPE_INFO(0, args, IS_ARRAY, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_call_method_if_exists, 0, 2, IS_MIXED, 0) ZEND_ARG_TYPE_INFO(0, obj, IS_OBJECT, 0) ZEND_ARG_TYPE_INFO(0, method, IS_STRING, 0) @@ -345,6 +350,7 @@ static ZEND_FUNCTION(zend_test_attribute_with_named_argument); static ZEND_FUNCTION(zend_get_current_func_name); static ZEND_FUNCTION(zend_call_method); static ZEND_FUNCTION(zend_object_init_with_constructor); +static ZEND_FUNCTION(zend_object_init_with_constructor_array); static ZEND_FUNCTION(zend_call_method_if_exists); static ZEND_FUNCTION(zend_test_zend_ini_parse_quantity); static ZEND_FUNCTION(zend_test_zend_ini_parse_uquantity); @@ -483,6 +489,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_get_current_func_name, arginfo_zend_get_current_func_name) ZEND_FE(zend_call_method, arginfo_zend_call_method) ZEND_FE(zend_object_init_with_constructor, arginfo_zend_object_init_with_constructor) + ZEND_FE(zend_object_init_with_constructor_array, arginfo_zend_object_init_with_constructor_array) ZEND_FE(zend_call_method_if_exists, arginfo_zend_call_method_if_exists) ZEND_FE(zend_test_zend_ini_parse_quantity, arginfo_zend_test_zend_ini_parse_quantity) ZEND_FE(zend_test_zend_ini_parse_uquantity, arginfo_zend_test_zend_ini_parse_uquantity) diff --git a/ext/zend_test/tests/zend_object_init_with_constructor_array.phpt b/ext/zend_test/tests/zend_object_init_with_constructor_array.phpt new file mode 100644 index 000000000000..0178114abcbe --- /dev/null +++ b/ext/zend_test/tests/zend_object_init_with_constructor_array.phpt @@ -0,0 +1,152 @@ +--TEST-- +Zend: Test object_init_with_constructor() API +--EXTENSIONS-- +zend_test +--FILE-- +<?php + +class TestUserWithConstructorArgs { + public function __construct(int $int_param, string $string_param) { + return new stdClass(); + } + public function __destruct() { + echo 'Destructor for ', __CLASS__, PHP_EOL; + } +} + +class TestUserWithConstructorNoParams { + public function __construct() { + return new stdClass(); + } + public function __destruct() { + echo 'Destructor for ', __CLASS__, PHP_EOL; + } +} + +class TestUserWithoutConstructor { + public function __destruct() { + echo 'Destructor for ', __CLASS__, PHP_EOL; + } +} + +$args = [5, 'str']; +$argsNamed = [5, 'string_param' => 'str']; +$argsNamedExtraUnknown = [5, 'string_param' => 'str', 'unknown' => 'unknown']; + +echo "Testing param passing\n"; +echo "Test TestUserWithConstructorArgs:\n"; +$o = zend_object_init_with_constructor_array("TestUserWithConstructorArgs", $args); +var_dump($o); +unset($o); + +$o = zend_object_init_with_constructor_array("TestUserWithConstructorArgs", $argsNamed); +var_dump($o); +unset($o); + +try { + $o = zend_object_init_with_constructor_array("TestUserWithConstructorArgs", $argsNamedExtraUnknown); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +echo "Test TestUserWithConstructorNoParams:\n"; +try { + $o = zend_object_init_with_constructor_array("TestUserWithConstructorNoParams", $args); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +try { + $o = zend_object_init_with_constructor_array("TestUserWithConstructorNoParams", $argsNamed); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +try { + $o = zend_object_init_with_constructor_array("TestUserWithConstructorNoParams", $argsNamedExtraUnknown); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +echo "Test TestUserWithoutConstructor:\n"; +$o = zend_object_init_with_constructor_array("TestUserWithoutConstructor", $args); +var_dump($o); +unset($o); + +try { + $o = zend_object_init_with_constructor_array("TestUserWithoutConstructor", $argsNamed); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +try { + $o = zend_object_init_with_constructor_array("TestUserWithoutConstructor", $argsNamedExtraUnknown); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +echo "Test _ZendTestMagicCall (internal class no ctor):\n"; +try { + $o = zend_object_init_with_constructor_array("_ZendTestMagicCall", $args); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +try { + $o = zend_object_init_with_constructor_array("_ZendTestMagicCall", $argsNamed); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +try { + $o = zend_object_init_with_constructor_array("_ZendTestMagicCall", $argsNamedExtraUnknown); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +Testing param passing +Test TestUserWithConstructorArgs: +object(TestUserWithConstructorArgs)#1 (0) { +} +Destructor for TestUserWithConstructorArgs +object(TestUserWithConstructorArgs)#1 (0) { +} +Destructor for TestUserWithConstructorArgs +Error: Unknown named parameter $unknown +Test TestUserWithConstructorNoParams: +object(TestUserWithConstructorNoParams)#1 (0) { +} +Destructor for TestUserWithConstructorNoParams +Error: Unknown named parameter $string_param +Error: Unknown named parameter $string_param +Test TestUserWithoutConstructor: +object(TestUserWithoutConstructor)#3 (0) { +} +Destructor for TestUserWithoutConstructor +Error: Unknown named parameter $string_param +Error: Unknown named parameter $string_param +Test _ZendTestMagicCall (internal class no ctor): +object(_ZendTestMagicCall)#2 (0) { +} +Error: Unknown named parameter $string_param +Error: Unknown named parameter $string_param diff --git a/ext/zend_test/tests/zend_object_init_with_constructor_classes_without_constructor.phpt b/ext/zend_test/tests/zend_object_init_with_constructor_classes_without_constructor.phpt index 1adaaafaa195..9d113fa6084f 100644 --- a/ext/zend_test/tests/zend_object_init_with_constructor_classes_without_constructor.phpt +++ b/ext/zend_test/tests/zend_object_init_with_constructor_classes_without_constructor.phpt @@ -98,6 +98,83 @@ try { echo $e::class, ': ', $e->getMessage(), PHP_EOL; } +echo "\n#### Splatting arrays with string key ####\n"; +$a = ['unknown' => "hello"]; +echo "Userland class:\n"; +echo "Using new:\n"; +try { + $o = new TestUserWithoutConstructor(...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} +echo "Using zend_object_init_with_constructor():\n"; +try { + $o = zend_object_init_with_constructor("TestUserWithoutConstructor", ...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +echo "Internal class:\n"; +echo "Using new:\n"; +try { + $o = new _ZendTestMagicCall(...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} +echo "Using zend_object_init_with_constructor():\n"; +try { + $o = zend_object_init_with_constructor("_ZendTestMagicCall", ...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + + +echo "\n#### Splatting arrays with int key ####\n"; +$a = [1 => "hello"]; +echo "Userland class:\n"; +echo "Using new:\n"; +try { + $o = new TestUserWithoutConstructor(...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} +echo "Using zend_object_init_with_constructor():\n"; +try { + $o = zend_object_init_with_constructor("TestUserWithoutConstructor", ...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +echo "Internal class:\n"; +echo "Using new:\n"; +try { + $o = new _ZendTestMagicCall(...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} +echo "Using zend_object_init_with_constructor():\n"; +try { + $o = zend_object_init_with_constructor("_ZendTestMagicCall", ...$a); + var_dump($o); + unset($o); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + ?> --EXPECT-- #### Passing no args #### @@ -147,3 +224,33 @@ Using new: Error: Unknown named parameter $unknown_param Using zend_object_init_with_constructor(): Error: Unknown named parameter $unknown_param + +#### Splatting arrays with string key #### +Userland class: +Using new: +Error: Unknown named parameter $unknown +Using zend_object_init_with_constructor(): +Error: Unknown named parameter $unknown +Internal class: +Using new: +Error: Unknown named parameter $unknown +Using zend_object_init_with_constructor(): +Error: Unknown named parameter $unknown + +#### Splatting arrays with int key #### +Userland class: +Using new: +object(TestUserWithoutConstructor)#2 (0) { +} +Destructor for TestUserWithoutConstructor +Using zend_object_init_with_constructor(): +object(TestUserWithoutConstructor)#2 (0) { +} +Destructor for TestUserWithoutConstructor +Internal class: +Using new: +object(_ZendTestMagicCall)#2 (0) { +} +Using zend_object_init_with_constructor(): +object(_ZendTestMagicCall)#2 (0) { +}

« previous php.cvs (#143416) next »