[PHP-DOC] Bug #14104 Updated: uasort doesn't work with static functions from inside classes to compare From: derick <email protected>
Date: 11/19/01

ID: 14104
Updated by: derick
Reported By: ernst <email protected>
Old Status: Open
Status: Closed
Bug Type: Documentation problem
Operating System: Linux RedHat 7.2
PHP Version: 4.0.6
New Comment:

Added documentation about this with the usort function.

Derick

Previous Comments:
------------------------------------------------------------------------

[2001-11-19 03:42:05] ernst <email protected>

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:34:34] ernst <email protected>

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.

------------------------------------------------------------------------

Edit this bug report at http://bugs.php.net/?id=14104&edit=1