php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73267 Order by callback changed
Submitted: 2016-10-07 21:21 UTC Modified: 2016-10-18 14:58 UTC
From: me at evertpot dot com Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 7.1.0RC3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: me at evertpot dot com
New email:
PHP Version: OS:

 

 [2016-10-07 21:21 UTC] me at evertpot dot com
Description:
------------
Hey guys,

I noticed one of my unittests failing on PHP 7.1. Took me a bit to figure out what was going on, but ended up figuring out that if you sort-by-callback, PHP 7.1 has a different order.

The use-case might sound a bit weird, and I'm certainly not suggesting that this is a bug. I merely wanted to suggest that maybe this might be added to the "Backwards compatibility changed" document. 

Test script:
---------------
https://3v4l.org/DWujs


<?php

$foo = [1,1];
$bar = [
   function() use (&$k) {
      echo "first!";
   },
   function() use (&$k) {
      echo "second!";
   },
];

array_multisort($foo, SORT_NUMERIC, $bar);

$bar[0]();

?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-08 09:10 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Package: Documentation problem +Package: Arrays related -Assigned To: +Assigned To: cmb
 [2016-10-08 09:10 UTC] cmb@php.net
Indeed, the sorting order has changed, but that is an
implementation detail that should not be relied upon anyway,
because:

| If two members compare as equal, their relative order in the
| sorted array is undefined.

That's already documented for all? other sorting functions, but
has been forgotten for array_multisort().

Adding a note to the changelog or migration guide regarding a
behavioral change of undefined behavior wouldn't make sense (the
behavior is still undefined).
 [2016-10-08 09:12 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=340377
Log: Fix #73267: Order by callback changed
 [2016-10-08 09:12 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2016-10-08 15:23 UTC] me at evertpot dot com
Hi cmb,

Thanks for the quick change. I definitely meant to mark it as a "documentation bug" but somehow that setting got lost.

I actually have a simpler test-case that demonstrates that it's not just equality of identical values:

https://3v4l.org/kH84K

It was a bit late yesterday, so I didn't catch this.

Now the result of this function might also (as you said) "undefined", and I readily admit that my unittest was wrong assuming a certain output, but it might still be a nice courtesy to include it in the migration documentation. Of course you can include in that document that relying on a sort order in the first place was wrong.
 [2016-10-18 14:58 UTC] cmb@php.net
I've noticed that the 7.0 UPGRADING has a respective note[1], so
it appears to be okay to also add notes to the 7.0 and 7.1
migration guides.

> I definitely meant to mark it as a "documentation bug" but
> somehow that setting got lost.

That's a nasty bug of the bug tracker. Previewing discards any
changes to the "Bug Type".

[1] <https://github.com/php/php-src/blob/PHP-7.0.12/UPGRADING#L434-L435>
 [2016-10-18 14:58 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=340557
Log: Fix #73267: Order by callback changed

While users are not supposed to rely on a stable sort anyway, it
may be helpful to explicitly mention possible sort order changes
in the migration guides.
 [2016-10-20 23:28 UTC] me at evertpot dot com
Thank, that's really awesome!
 [2020-02-07 06:06 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=e1a00d71720c1d76b925283125eb544b716b10e8
Log: Fix #73267: Order by callback changed
 [2020-02-07 06:06 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=5c65942664074e1a707e89fbc6887860287d60e9
Log: Fix #73267: Order by callback changed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 10:01:31 2024 UTC