1--TEST--
2Test uasort() function : object functionality - sort diff. objects
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 * This testcase tests uasort() functionality with different objects
12 * Objects of different classes:
13 *  simple class,
14 *  child class,
15 *  empty class &
16 *  static class
17 */
18
19echo "*** Testing uasort() : object functionality ***\n";
20
21// comparison function
22/* Prototype : int cmp_function(mixed $value1, mixed $value2)
23 * Parameters : $value1 and $value2 - values to be compared
24 * Return value : 0 - if both values are same
25 *                1 - if value1 is greater than value2
26 *               -1 - if value1 is less than value2
27 * Description : compares value1 and value2
28 */
29function cmp_function($value1, $value2)
30{
31  if($value1 == $value2) {
32    return 0;
33  }
34  else if($value1 > $value2) {
35    return 1;
36  }
37  else
38    return -1;
39}
40
41
42// Simple class with single member variable
43class SimpleClass
44{
45  private $int_value;
46
47  public function __construct($value) {
48    $this->int_value = $value;
49  }
50
51}
52
53// Class without any member
54class EmptyClass
55{
56}
57
58// Class with static member
59class StaticClass
60{
61  public static $static_value;
62  public function __construct($value) {
63    StaticClass::$static_value = $value;
64  }
65}
66
67// Abstract class
68abstract class AbstractClass
69{
70  public $pub_value;
71  public abstract function abstractMethod();
72}
73
74// Child class extending abstract class
75class ChildClass extends AbstractClass
76{
77  public $child_value = 100;
78  public function abstractMethod() {
79    $pub_value = 5;
80  }
81  public function __construct($value) {
82    $this->child_value = $value;
83  }
84}
85
86// Testing uasort with StaticClass objects as elements of 'array_arg'
87echo "-- Testing uasort() with StaticClass objects --\n";
88$array_arg = array(
89  0 => new StaticClass(20),
90  1 => new StaticClass(50),
91  2 => new StaticClass(15),
92  3 => new StaticClass(70),
93);
94var_dump( uasort($array_arg, 'cmp_function') );
95var_dump($array_arg);
96
97// Testing uasort with EmptyClass objects as elements of 'array_arg'
98echo "-- Testing uasort() with EmptyClass objects --\n";
99$array_arg = array(
100  0 => new EmptyClass(),
101  1 => new EmptyClass(),
102  2 => new EmptyClass(),
103  3 => new EmptyClass(),
104);
105var_dump( uasort($array_arg, 'cmp_function') );
106var_dump($array_arg);
107
108// Testing uasort with ChildClass objects as elements of 'array_arg'
109echo "-- Testing uasort() with ChildClass objects --\n";
110$array_arg = array(
111  0 => new ChildClass(20),
112  1 => new ChildClass(500),
113  2 => new ChildClass(15),
114  3 => new ChildClass(700),
115);
116var_dump( uasort($array_arg, 'cmp_function') );
117var_dump($array_arg);
118
119echo "Done"
120?>
121--EXPECTF--
122*** Testing uasort() : object functionality ***
123-- Testing uasort() with StaticClass objects --
124bool(true)
125array(4) {
126  [3]=>
127  object(StaticClass)#%d (0) {
128  }
129  [2]=>
130  object(StaticClass)#%d (0) {
131  }
132  [1]=>
133  object(StaticClass)#%d (0) {
134  }
135  [0]=>
136  object(StaticClass)#%d (0) {
137  }
138}
139-- Testing uasort() with EmptyClass objects --
140bool(true)
141array(4) {
142  [3]=>
143  object(EmptyClass)#%d (0) {
144  }
145  [2]=>
146  object(EmptyClass)#%d (0) {
147  }
148  [1]=>
149  object(EmptyClass)#%d (0) {
150  }
151  [0]=>
152  object(EmptyClass)#%d (0) {
153  }
154}
155-- Testing uasort() with ChildClass objects --
156bool(true)
157array(4) {
158  [2]=>
159  object(ChildClass)#%d (2) {
160    ["child_value"]=>
161    int(15)
162    ["pub_value"]=>
163    NULL
164  }
165  [0]=>
166  object(ChildClass)#%d (2) {
167    ["child_value"]=>
168    int(20)
169    ["pub_value"]=>
170    NULL
171  }
172  [1]=>
173  object(ChildClass)#%d (2) {
174    ["child_value"]=>
175    int(500)
176    ["pub_value"]=>
177    NULL
178  }
179  [3]=>
180  object(ChildClass)#%d (2) {
181    ["child_value"]=>
182    int(700)
183    ["pub_value"]=>
184    NULL
185  }
186}
187Done
188