|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 08:00:02 2025 UTC |
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. :)