com php-src: Allow array_merge() / array_merge_re cursive() without arguments: UPGRADING ext/standa rd/array.c ext/standard/basic_functions.c
ext/standar d/tests/array/array_merge.phpt ext/standard/tests/array/a rray_merge_recursive_basic1.phpt ext/standard/tests
/array/array_merge_recursive_error.phpt
| From: | Nikita Popov | Date: | Tue, 28 May 2019 09:14:15 +0000 |
| Subject: | com php-src: Allow array_merge() / array_merge_re cursive() without arguments: UPGRADING ext/standa rd/array.c ext/standard/basic_functions.c ext/standar d/tests/array/array_merge.phpt ext/standard/tests/array/a rray_merge_recursive_basic1.phpt ext/standard/tests /array/array_merge_recursive_error.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-106384@lists.php.net to get a copy of this message | ||
Commit: 77cf3d7b1100dbb2b441b2a75f21b4e8ee0cb9b1
Author: Dik Takken <d.h.j.takken@xs4all.nl> Thu, 16 May 2019 22:08:08 +0200
Committer: Nikita Popov <nikita.ppv@gmail.com> Tue, 28 May 2019 11:14:15 +0200
Parents: 072f28938f1c8860432c22c03ce7bdc7e8112e61
Branches: PHP-7.4 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=77cf3d7b1100dbb2b441b2a75f21b4e8ee0cb9b1
Log:
Allow array_merge() / array_merge_recursive() without arguments
This allows writing
array_merge(...$arrays)
instead of
array_merge([], ...$arrays)
and is in line with similar changes to array_push() and array_unshift()
in PHP 7.3.
Closes GH-4175.
Changed paths:
M UPGRADING
M ext/standard/array.c
M ext/standard/basic_functions.c
M ext/standard/tests/array/array_merge.phpt
M ext/standard/tests/array/array_merge_recursive_basic1.phpt
D ext/standard/tests/array/array_merge_recursive_error.phpt
Diff:
diff --git a/UPGRADING b/UPGRADING
index 058a5d0..e9201f2 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -259,6 +259,10 @@ PHP 7.4 UPGRADE NOTES
RFC: https://wiki.php.net/rfc/custom_object_serialization
+ . array_merge() and array_merge_recursive() may now be called without any
+ arguments, in which case they will return an empty array. This is useful
+ in conjunction with the spread operator, e.g. array_merge(...$arrays).
+
========================================
3. Changes in SAPI modules
========================================
diff --git a/ext/standard/array.c b/ext/standard/array.c
index 34f1284..652a659 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -3789,10 +3789,14 @@ static zend_always_inline void
php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
HashTable *src, *dest;
uint32_t count = 0;
- ZEND_PARSE_PARAMETERS_START(1, -1)
+ ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_VARIADIC('+', args, argc)
ZEND_PARSE_PARAMETERS_END();
+ if (argc == 0) {
+ RETURN_EMPTY_ARRAY();
+ }
+
for (i = 0; i < argc; i++) {
zval *arg = args + i;
@@ -3882,7 +3886,7 @@ static zend_always_inline void
php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
}
/* }}} */
-/* {{{ proto array array_merge(array arr1 [, array ...])
+/* {{{ proto array array_merge([array ...])
Merges elements from passed arrays into one array */
PHP_FUNCTION(array_merge)
{
@@ -3890,7 +3894,7 @@ PHP_FUNCTION(array_merge)
}
/* }}} */
-/* {{{ proto array array_merge_recursive(array arr1 [, array ...])
+/* {{{ proto array array_merge_recursive([array ...])
Recursively merges elements from passed arrays into one array */
PHP_FUNCTION(array_merge_recursive)
{
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 0ec4fb3..920974f 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -379,13 +379,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_slice, 0, 0, 2)
ZEND_ARG_INFO(0, preserve_keys)
ZEND_END_ARG_INFO()
-ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge, 0, 0, 1)
- ZEND_ARG_INFO(0, arr1) /* ARRAY_INFO(0, arg, 0) */
+ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge, 0, 0, 0)
ZEND_ARG_VARIADIC_INFO(0, arrays)
ZEND_END_ARG_INFO()
-ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge_recursive, 0, 0, 1)
- ZEND_ARG_INFO(0, arr1) /* ARRAY_INFO(0, arg, 0) */
+ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge_recursive, 0, 0, 0)
ZEND_ARG_VARIADIC_INFO(0, arrays)
ZEND_END_ARG_INFO()
diff --git a/ext/standard/tests/array/array_merge.phpt b/ext/standard/tests/array/array_merge.phpt
index e6a8096..0afc208 100644
--- a/ext/standard/tests/array/array_merge.phpt
+++ b/ext/standard/tests/array/array_merge.phpt
@@ -80,11 +80,13 @@ var_dump(array_merge($begin_array[4], (array)"type1", (array)10,
(array)12.34));
echo "\n*** Testing error conditions ***";
/* Invalid arguments */
-var_dump(array_merge());
var_dump(array_merge(100, 200));
var_dump(array_merge($begin_array[0], $begin_array[1], 100));
var_dump(array_merge($begin_array[0], $begin_array[1], $arr4));
+echo "\n*** Testing array_merge without any arguments ***\n";
+var_dump(array_merge());
+
echo "Done\n";
?>
--EXPECTF--
@@ -746,9 +748,6 @@ array(7) {
}
*** Testing error conditions ***
-Warning: array_merge() expects at least 1 parameter, 0 given in %s on line %d
-NULL
-
Warning: array_merge(): Expected parameter 1 to be an array, int given in %s on line %d
NULL
@@ -759,4 +758,8 @@ Notice: Undefined variable: arr4 in %s on line %d
Warning: array_merge(): Expected parameter 3 to be an array, null given in %s on line %d
NULL
+
+*** Testing array_merge without any arguments ***
+array(0) {
+}
Done
diff --git a/ext/standard/tests/array/array_merge_recursive_basic1.phpt
b/ext/standard/tests/array/array_merge_recursive_basic1.phpt
index efda3d5..b163a84 100644
--- a/ext/standard/tests/array/array_merge_recursive_basic1.phpt
+++ b/ext/standard/tests/array/array_merge_recursive_basic1.phpt
@@ -14,6 +14,10 @@ $arr1 = array(1, array(1, 2));
$arr2 = array(3, array("hello", 'world'));
$arr3 = array(array(6, 7), array("str1", 'str2'));
+// Calling array_merge_recursive() without arguments
+echo "-- Without arguments --\n";
+var_dump( array_merge_recursive() );
+
// Calling array_merge_recursive() with default arguments
echo "-- With default argument --\n";
var_dump( array_merge_recursive($arr1) );
@@ -27,6 +31,9 @@ echo "Done";
?>
--EXPECT--
*** Testing array_merge_recursive() : array with default keys ***
+-- Without arguments --
+array(0) {
+}
-- With default argument --
array(2) {
[0]=>
diff --git a/ext/standard/tests/array/array_merge_recursive_error.phpt
b/ext/standard/tests/array/array_merge_recursive_error.phpt
deleted file mode 100644
index ffa9cc9..0000000
--- a/ext/standard/tests/array/array_merge_recursive_error.phpt
+++ /dev/null
@@ -1,25 +0,0 @@
---TEST--
-Test array_merge_recursive() function : error conditions
---FILE--
-<?php
-/* Prototype : array array_merge_recursive(array $arr1[, array $...])
- * Description: Recursively merges elements from passed arrays into one array
- * Source code: ext/standard/array.c
- */
-
-echo "*** Testing array_merge_recursive() : error conditions ***\n";
-
-// Zero arguments
-echo "\n-- Testing array_merge_recursive() function with Zero arguments --\n";
-var_dump( array_merge_recursive() );
-
-echo "Done";
-?>
---EXPECTF--
-*** Testing array_merge_recursive() : error conditions ***
-
--- Testing array_merge_recursive() function with Zero arguments --
-
-Warning: array_merge_recursive() expects at least 1 parameter, 0 given in %s on line %d
-NULL
-Done