Bug #64580 [Com]: sort() triggers type-conversion notice
| From: | hanskrentel at yahoo dot de | Date: | Sat, 06 Apr 2013 14:50:48 +0000 |
| Subject: | Bug #64580 [Com]: sort() triggers type-conversion notice | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-178322@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64580&edit=1
ID: 64580
Comment by: hanskrentel at yahoo dot de
Reported by: hanskrentel at yahoo dot de
Summary: sort() triggers type-conversion notice
Status: Not a bug
Type: Bug
Package: Arrays related
PHP Version: 5.4.13
Block user comment: N
Private report: N
New Comment:
To demonstrate that sort does work (maybe not a full proof but a good demo at
least) - despite the fact the object can not be converted to int, it is treated as
if it would be the integer one.
Demo: http://3v4l.org/2XBBi
Output for 5.1.0 - 5.5.0beta2:
sort caused 43 error(s) in an array of 24 member(s) of which 4 are object(s).
sort()-result: 0, 0, 1, 1, 1, 1, obj(1), obj(1.2), obj(2), obj(2.1), 2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 3
Output for 4.3.0 - 5.0.5:
sort caused 0 error(s) in an array of 24 member(s) of which 4 are object(s).
sort()-result: 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, obj(1),
obj(1.2), obj(2), obj(2.1)
The earlier versions show how arrays are still treated today. Without any warning
and larger than anything else.
Previous Comments:
------------------------------------------------------------------------
[2013-04-04 12:50:49] hanskrentel at yahoo dot de
The
sort() function explicitly states:
> don't change types
If no types are changed, why do I get a type conversion notice?
I can understand that for comparing those values, a sort-value of each
array member must be obtained to create to do sorting, however,
as long as the sort is successful, this should not create any notices.
------------------------------------------------------------------------
[2013-04-04 12:47:18] nikic@php.net
Nothing to do with sort() this is just the usual behavior of the "<" operator. Try
doing just "1 < new stdClass" and you'll get the same notice.
------------------------------------------------------------------------
[2013-04-04 12:43:42] hanskrentel at yahoo dot de
Description:
------------
Under specific circumstances the sort() function with the SORT_REGULAR sort-
flag triggers type-conversion notices even it is documented that no type-
conversion is done[1]:
> SORT_REGULAR - compare items normally (don't change types)
This notice is *only* given if an object value is sorted with an integer or
float.
The notices for these two cases are as following:
> Notice: Object of class stdClass could not be converted to int
> Notice: Object of class stdClass could not be converted to double
Those errors are triggered in /Zend (lxr search http://goo.gl/Zu7Zl).
The sort() function returns TRUE despite the Notice and as far I was able to
find out, the sorting is done, so this looks correct to me.
Also this error only happens with sorting objects with integers and floats. I
also tested against NULL, boolean (TRUE/FALSE), string, array, object and
resource and these did not trigger the notice.
I would not classify this as documentation bug because these are very specific
circumstances which makes me assume that this is not the intended behavior.
Something might have been just overlooked for these two cases with
SORT_REGULAR.
It should be said that there are related issues documented in #54980 and
#54259 over which I originally stumbled.
[1] http://php.net/sort
Test script:
---------------
<?php
$subject = [1, (object) ['prop' => 'value']];
sort($subject);
Actual result:
--------------
Notice: Object of class stdClass could not be converted to int in [...] on line 3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64580&edit=1