2 /* Prototype : bool usort(&array $array_arg, string $cmp_function)
3 * Description: Sort an array by values using a user-defined comparison function
4 * Source code: ext/standard/array.c
8 * Pass an array of objects which are either:
12 * to test behaviour of usort()
15 function cmp_function($value1, $value2)
17 if($value1 == $value2) {
20 else if($value1 > $value2) {
27 // Class without any member
32 // Class with static member
35 public static $static_value;
36 public function __construct($value) {
37 StaticClass
::$static_value = $value;
42 abstract class AbstractClass
45 public abstract function abstractMethod();
48 // Child class extending abstract class
49 class ChildClass
extends AbstractClass
51 public $child_value = 100;
52 public function abstractMethod() {
55 public function __construct($value) {
56 $this->child_value
= $value;
59 <<__EntryPoint
>> function main(): void
{
60 echo "*** Testing usort() : object functionality ***\n";
62 // Testing uasort with StaticClass objects as elements of 'array_arg'
63 echo "-- Testing usort() with StaticClass objects --\n";
65 0 => new StaticClass(20),
66 1 => new StaticClass(50),
67 2 => new StaticClass(15),
68 3 => new StaticClass(70),
70 var_dump( usort(&$array_arg, fun('cmp_function')) );
73 // Testing uasort with EmptyClass objects as elements of 'array_arg'
74 echo "-- Testing usort() with EmptyClass objects --\n";
76 0 => new EmptyClass(),
77 1 => new EmptyClass(),
78 2 => new EmptyClass(),
79 3 => new EmptyClass(),
81 var_dump( usort(&$array_arg, fun('cmp_function')) );
84 // Testing uasort with ChildClass objects as elements of 'array_arg'
85 echo "-- Testing usort() with ChildClass objects --\n";
87 0 => new ChildClass(20),
88 1 => new ChildClass(500),
89 2 => new ChildClass(15),
90 3 => new ChildClass(700),
92 var_dump( usort(&$array_arg, fun('cmp_function')) );