php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #14104 uasort doesn't work with static functions from inside classes to compare
Submitted: 2001-11-19 03:34 UTC Modified: 2001-11-19 03:56 UTC
From: ernst at baschny dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.6 OS: Linux RedHat 7.2
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ernst at baschny dot de
New email:
PHP Version: OS:

 

 [2001-11-19 03:34 UTC] ernst at baschny dot de
If I have a static function inside a class that does the comparasion of two objects, I haven't found a way of passing a reference to it in uasort (u.*sort). I tried "Classname::function", also "Classname->function".

If the function doesn't exists, not even an error is produced, so its very difficult to debug such things.

Heres an example of what doesn't work:

class TestObj {
    var $name;
    function TestObj($name) { $this->name = $name; }

    /* This is the static comparing function: */
    function cmp_obj($a, $b) {
        $al = strtolower($a->name);
        $bl = strtolower($b->name);
        if ($al == $bl) return 0;
        return ($al > $bl) ? +1 : -1;
    }

}

$a[] = new TestObj("b");
$a[] = new TestObj("a");
$a[] = new TestObj("d");

uasort($a, "TestObj::cmp_obj");

printf("<pre>\n");
print_r($a);
printf("</pre>\n");

-> Result: NOT SORTED.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-19 03:42 UTC] ernst at baschny dot de
Well, in a different context (call_user_func) I found out that it is indeed possible, but not documented.

To pass a reference to a function inside a Class to a u.*sort Function, pass it as an array with two elements (the class name and the function name).

For the example above, this does it:

uasort($a, array("TestObj", "cmp_obj"));

Undocumented. So this bug is now a documentation bug. :)
 [2001-11-19 03:56 UTC] derick@php.net
Added documentation about this with the usort function.

Derick
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 07:01:33 2025 UTC