Req #71658 [Opn->Csd]: Change array_merge() to merge valid parameters if any is invalid

From: Date: Wed, 06 Jan 2021 16:19:55 +0000
Subject: Req #71658 [Opn->Csd]: Change array_merge() to merge valid parameters if any is invalid
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-231412@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71658&edit=1

 ID:                 71658
 Updated by:         cmb@php.net
 Reported by:        wadev at mailbox dot hu
 Summary:            Change array_merge() to merge valid parameters if
                     any is invalid
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            Arrays related
 Operating System:   Any
 PHP Version:        7.0.3
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

Well, on further consideration this proposal makes no sense to me.
array_merge() excepts arrays, so passing something which is not an
array, and cannot be coerced to one, respectively, is just a
programming error.


Previous Comments:
------------------------------------------------------------------------
[2016-08-04 17:54:20] cmb@php.net

I don't see why array_merge() should behave differently than
array_intersect(), array_diff() etc. (and I wouldn't change them
all), but if you consider that an improvement, feel free to start
the RFC process (see <https://wiki.php.net/rfc/howto>).

------------------------------------------------------------------------
[2016-02-24 11:24:42] wadev at mailbox dot hu

Description:
------------
array_merge() function returns NULL if any of the arguments are not arrays.
This makes for a lot of boilerplate code / guard clauses, such that I, more often than not, rather
end up using foreach for merging instead.

I don't know if I am the only one who expects it to either:

- return a merged array containing all of the valid arrays' keys/values, or
- return a merged array containing all of the valid arrays' keys/values before the first
"invalid" argument.

This latter might (or might not) be preferrable on performance considerations.

Please propose a change and/or vote on proposal if this is even possible.
If so, I may make a patch if needed.

EDIT: as this seems similar to #64909, another proposal would be the ability to change the emitted
E_WARNING into a catchable fatal error.

This is quite strange that in such case we could catch a java.lang.NullPointerException in Java, but
nothing in PHP; quite counter-productive.


Test script:
---------------
<?php
var_dump(array_merge(['what', 'ever'], false));

Expected result:
----------------
array(2) {
  [0]=>
  string(4) "what"
  [1]=>
  string(4) "ever"
}

Actual result:
--------------
<br />
<b>Warning</b>:  array_merge(): Argument #2 is not an array in
<b>[...][...]</b> on line <b>2</b><br />
NULL



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71658&edit=1


Thread (3 messages)

« previous php.bugs (#231412) next »