php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64580 sort() triggers type-conversion notice
Submitted: 2013-04-04 12:43 UTC Modified: 2013-04-04 12:47 UTC
From: hanskrentel at yahoo dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.13 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hanskrentel at yahoo dot de
New email:
PHP Version: OS:

 

 [2013-04-04 12:43 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-04 12:47 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2013-04-04 12:47 UTC] 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:50 UTC] 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-06 14:50 UTC] hanskrentel at yahoo dot de
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC