PHP: usort() – invalid comparison function error

If you are using usort() within a class object and you have the call back function correctly defined you might see this error:

Warning: usort() [function.usort]: Invalid comparison function....

The reason for this error is that usort is looking in the wrong place for the function. You have to tell usort the correct context to call the callback function from. You pass the context as an array value in the callback parameter. More info about callbacks here

If your callback function is a member function of class then call it this way:

usort( $data, array( $this, 'func_name' ) );

If the callback method is static then you can call it this way:

usort( $data, array(__class__, 'func_name' ) );

Leave a comment