1--TEST--
2Test uasort() function : object functionality
3--FILE--
4<?php
5/* Prototype  : bool uasort(array $array_arg, string $cmp_function)
6 * Description: Sort an array with a user-defined comparison function and maintain index association
7 * Source code: ext/standard/array.c
8*/
9
10/*
11 * Testing uasort() function with the array of objects
12 * array of objects which has only one member variable & more than one member variables
13 */
14
15echo "*** Testing uasort() : object functionality ***\n";
16
17// comparison function
18/* Prototype : int cmp(mixed $value1, mixed $value2)
19 * Parameters : $value1 and $value2 - values to be compared
20 * Return value : 0 - if both values are same
21 *                1 - if value1 is greater than value2
22 *               -1 - if value1 is less than value3
23 * Description : compares value1 and value2
24 */
25function simple_cmp($value1, $value2)
26{
27  if($value1 == $value2) {
28    return 0;
29  }
30  else if($value1 > $value2) {
31    return 1;
32  }
33  else
34    return -1;
35}
36
37// comparison function for SimpleClass2 objects which has more than one members
38function multiple_cmp($value1, $value2)
39{
40  if($value1->getValue() == $value2->getValue())
41    return 0;
42  else if($value1->getValue() > $value2->getValue())
43    return 1;
44  else
45    return -1;
46}
47
48// Simple class with single member variable
49class SimpleClass1
50{
51  private $int_value;
52
53  public function __construct($value) {
54    $this->int_value = $value;
55  }
56}
57
58// Simple class with more than one member variables
59class SimpleClass2
60{
61  private $int_value;
62  protected $float_value;
63  public $string_value;
64  public function __construct($int, $float, $str) {
65    $this->int_value = $int;
66    $this->float_value = $float;
67    $this->string_value = $str;
68  }
69  public function getValue() {
70    return $this->int_value;
71  }
72}
73
74// array of SimpleClass objects with only one member
75$array_arg = array(
76  0 => new SimpleClass1(10),
77  1 => new SimpleClass1(1),
78  2 => new SimpleClass1(100),
79  3 => new SimpleClass1(50)
80);
81var_dump( uasort($array_arg, 'simple_cmp') );
82var_dump($array_arg);
83
84// array of SimpleClass objects having more than one members
85$array_arg = array(
86  0 => new SimpleClass2(2, 3.4, "mango"),
87  1 => new SimpleClass2(10, 1.2, "apple"),
88  2 => new SimpleClass2(5, 2.5, "orange"),
89);
90var_dump( uasort($array_arg, 'multiple_cmp') );
91var_dump($array_arg);
92
93echo "Done"
94?>
95--EXPECT--
96*** Testing uasort() : object functionality ***
97bool(true)
98array(4) {
99  [1]=>
100  object(SimpleClass1)#2 (1) {
101    ["int_value":"SimpleClass1":private]=>
102    int(1)
103  }
104  [0]=>
105  object(SimpleClass1)#1 (1) {
106    ["int_value":"SimpleClass1":private]=>
107    int(10)
108  }
109  [3]=>
110  object(SimpleClass1)#4 (1) {
111    ["int_value":"SimpleClass1":private]=>
112    int(50)
113  }
114  [2]=>
115  object(SimpleClass1)#3 (1) {
116    ["int_value":"SimpleClass1":private]=>
117    int(100)
118  }
119}
120bool(true)
121array(3) {
122  [0]=>
123  object(SimpleClass2)#5 (3) {
124    ["int_value":"SimpleClass2":private]=>
125    int(2)
126    ["float_value":protected]=>
127    float(3.4)
128    ["string_value"]=>
129    string(5) "mango"
130  }
131  [2]=>
132  object(SimpleClass2)#7 (3) {
133    ["int_value":"SimpleClass2":private]=>
134    int(5)
135    ["float_value":protected]=>
136    float(2.5)
137    ["string_value"]=>
138    string(6) "orange"
139  }
140  [1]=>
141  object(SimpleClass2)#6 (3) {
142    ["int_value":"SimpleClass2":private]=>
143    int(10)
144    ["float_value":protected]=>
145    float(1.2)
146    ["string_value"]=>
147    string(5) "apple"
148  }
149}
150Done
151