convert ***sort builtins to use inout instead of references
[hiphop-php.git] / hphp / test / zend / good / ext / standard / tests / array / usort_object2.php
blob42262cb92429adce5e24cb0fcd3237e584f53348
1 <?hh
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
5 */
7 /*
8 * Pass an array of objects which are either:
9 * 1. Empty
10 * 2. Static
11 * 2. Inherited
12 * to test behaviour of usort()
15 function cmp_function($value1, $value2)
17 if($value1 == $value2) {
18 return 0;
20 else if($value1 > $value2) {
21 return 1;
23 else
24 return -1;
27 // Class without any member
28 class EmptyClass
32 // Class with static member
33 class StaticClass
35 public static $static_value;
36 public function __construct($value) {
37 StaticClass::$static_value = $value;
41 // Abstract class
42 abstract class AbstractClass
44 public $pub_value;
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() {
53 $pub_value = 5;
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";
64 $array_arg = array(
65 0 => new StaticClass(20),
66 1 => new StaticClass(50),
67 2 => new StaticClass(15),
68 3 => new StaticClass(70),
70 var_dump( usort(inout $array_arg, fun('cmp_function')) );
71 var_dump($array_arg);
73 // Testing uasort with EmptyClass objects as elements of 'array_arg'
74 echo "-- Testing usort() with EmptyClass objects --\n";
75 $array_arg = array(
76 0 => new EmptyClass(),
77 1 => new EmptyClass(),
78 2 => new EmptyClass(),
79 3 => new EmptyClass(),
81 var_dump( usort(inout $array_arg, fun('cmp_function')) );
82 var_dump($array_arg);
84 // Testing uasort with ChildClass objects as elements of 'array_arg'
85 echo "-- Testing usort() with ChildClass objects --\n";
86 $array_arg = array(
87 0 => new ChildClass(20),
88 1 => new ChildClass(500),
89 2 => new ChildClass(15),
90 3 => new ChildClass(700),
92 var_dump( usort(inout $array_arg, fun('cmp_function')) );
93 var_dump($array_arg);
94 echo "===DONE===\n";