convert ***sort builtins to use inout instead of references
[hiphop-php.git] / hphp / test / zend / good / ext / standard / tests / array / uasort_object1.php
blobc3d8d4babdf3a48dbbd1dc9947b2cadc2ccada0e
1 <?hh
2 /* Prototype : bool uasort(array $array_arg, string $cmp_function)
3 * Description: Sort an array with a user-defined comparison function and maintain index association
4 * Source code: ext/standard/array.c
5 */
7 /*
8 * Testing uasort() function with the array of objects
9 * array of objects which has only one member variable & more than one member variables
12 // comparison function
13 /* Prototype : int cmp(mixed $value1, mixed $value2)
14 * Parameters : $value1 and $value2 - values to be compared
15 * Return value : 0 - if both values are same
16 * 1 - if value1 is greater than value2
17 * -1 - if value1 is less than value3
18 * Description : compares value1 and value2
20 function simple_cmp($value1, $value2)
22 if($value1 == $value2) {
23 return 0;
25 else if($value1 > $value2) {
26 return 1;
28 else
29 return -1;
32 // comparison function for SimpleClass2 objects which has more than one members
33 function multiple_cmp($value1, $value2)
35 if($value1->getValue() == $value2->getValue())
36 return 0;
37 else if($value1->getValue() > $value2->getValue())
38 return 1;
39 else
40 return -1;
43 // Simple class with single member variable
44 class SimpleClass1
46 private $int_value;
48 public function __construct($value) {
49 $this->int_value = $value;
53 // Simple class with more than one member variables
54 class SimpleClass2
56 private $int_value;
57 protected $float_value;
58 public $string_value;
59 public function __construct($int, $float, $str) {
60 $this->int_value = $int;
61 $this->float_value = $float;
62 $this->string_value = $str;
64 public function getValue() {
65 return $this->int_value;
68 <<__EntryPoint>> function main(): void {
69 echo "*** Testing uasort() : object functionality ***\n";
71 // array of SimpleClass objects with only one member
72 $array_arg = array(
73 0 => new SimpleClass1(10),
74 1 => new SimpleClass1(1),
75 2 => new SimpleClass1(100),
76 3 => new SimpleClass1(50)
78 var_dump( uasort(inout $array_arg, fun('simple_cmp')) );
79 var_dump($array_arg);
81 // array of SimpleClass objects having more than one members
82 $array_arg = array(
83 0 => new SimpleClass2(2, 3.4, "mango"),
84 1 => new SimpleClass2(10, 1.2, "apple"),
85 2 => new SimpleClass2(5, 2.5, "orange"),
87 var_dump( uasort(inout $array_arg, fun('multiple_cmp')) );
88 var_dump($array_arg);
90 echo "Done";