Bug #50688 [Com]: Using exceptions inside usort() callback function causes a warning
| From: | jakub dot lopuszanski at nasza-klasa dot pl | Date: | Tue, 09 Jul 2013 07:21:19 +0000 |
| Subject: | Bug #50688 [Com]: Using exceptions inside usort() callback function causes a warning | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-180136@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=50688&edit=1
ID: 50688
Comment by: jakub dot lopuszanski at nasza-klasa dot pl
Reported by: jcampbell at remindermedia dot com
Summary: Using exceptions inside usort() callback function
causes a warning
Status: Assigned
Type: Bug
Package: Arrays related
Operating System: Fedora Core 12
PHP Version: 5.*, 6
Assigned To: stas
Block user comment: N
Private report: N
New Comment:
I'd like to add, that you do not have to throw an exception to get this warning.
Mere creating it, also triggers the warning, as in:
<?php
function comp($a,$b){
@new Exception("dupa");
}
$a =array(1,2,3);
usort($a, 'comp');
var_dump($a);
?>
PHP Warning: usort(): Array was modified by the user comparison function in
/home/jlopuszanski/test.php on line 6
Previous Comments:
------------------------------------------------------------------------
[2013-06-17 11:11:07] andrejs dot verza at gmail dot com
Php 5.4.16 also fails with this.
Still the same status for 3 and a half years old bug?!
------------------------------------------------------------------------
[2012-08-08 17:53:58] mbrowne83 at gmail dot com
This will probably be obvious to most, but I just wanted to mention that you can always prefix the
usort function with the @ symbol to prevent the warning...of course that would also suppress any
other types of notices or warnings that might occcur anywhere within the sorting function...
------------------------------------------------------------------------
[2012-02-24 18:04:02] keith at breadvault dot com
This same problem arises when using Mockery to mock the object whose method is
being used by usort(), even though the method itself neither is mocked nor handles
any exceptions. The proxy generated by Mockery must wrap the target class's
methods with some exception-handling code.
Unfortunately this forced me to code a workaround that would not use usort. My
hack extracts from the objects in the array the values being sorted on, sorts that
array of values using asort() (to preserve the keys), and finally rebuilds the
list of objects using the keys in the order that they appear in the asorted list
of values. Yuck.
------------------------------------------------------------------------
[2012-02-21 22:56:31] eric_haney at yahoo dot com
It took me a while to figure out that some code called from usort was throwing, catching, and
(gracefully) handling an Exception. Then I found this post. Quite frustrating.
I turned off warnings with ini_set before calling usort, then turned them on again after. This is
an effective workaround for now, but I'd love to clean that nastiness out of my code.
It is also my opinion that usort should be allowed to change the elements in the array. EG: an
instance variable of an object may be lazy-loaded as a result of a method call from within a usort
callback. Should a warning really be issued in that case?
------------------------------------------------------------------------
[2011-10-10 21:44:56] poehler at interworx dot com
This bug is still present as of PHP 5.3.8, we ran into it today and spent most of a day trying to
figure out what was causing the error message "Array was modified by the user comparison
function", when CLEARLY, NOTHING was changing the array at all!
The exception was not thrown/caught directly in the usort function but rather in a constructor of a
class that was called about 3 or 4 functions deep from the usort, making it very difficult to track
down.
After finally figuring out the exception was somehow related, we searched google and found this bug
report. I'm sure we can agree that the minor act of catching an exception should not result in
usort throwing a warning message. This bug is a huge timewaster :(
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=50688
--
Edit this bug report at https://bugs.php.net/bug.php?id=50688&edit=1