1--TEST-- 2Test asort() function : object functionality - sort objects 3--FILE-- 4<?php 5/* 6 * testing asort() by providing integer/string object arrays with following flag values 7 * 1. Default flag value 8 * 2. SORT_REGULAR - compare items normally 9*/ 10 11echo "*** Testing asort() : object functionality ***\n"; 12 13// class declaration for integer objects 14class for_integer_asort 15{ 16 public $class_value; 17 // initializing object member value 18 function __construct($value){ 19 $this->class_value = $value; 20 } 21 22} 23 24// class declaration for string objects 25class for_string_asort 26{ 27 public $class_value; 28 // initializing object member value 29 function __construct($value){ 30 $this->class_value = $value; 31 } 32 33 // return string value 34 function __tostring() { 35 return (string)$this->value; 36 } 37 38} 39 40// array of integer objects 41$unsorted_int_obj = array ( 42 1 => new for_integer_asort(11), 2 => new for_integer_asort(66), 43 3 => new for_integer_asort(23), 4 => new for_integer_asort(-5), 44 5 => new for_integer_asort(0.001), 6 => new for_integer_asort(0) 45); 46 47// array of string objects 48$unsorted_str_obj = array ( 49 "a" => new for_string_asort("axx"), "b" => new for_string_asort("t"), 50 "c" => new for_string_asort("w"), "d" => new for_string_asort("py"), 51 "e" => new for_string_asort("apple"), "f" => new for_string_asort("Orange"), 52 "g" => new for_string_asort("Lemon"), "h" => new for_string_asort("aPPle") 53); 54 55 56echo "\n-- Testing asort() by supplying various object arrays, 'flag' value is default --\n"; 57 58// testing asort() function by supplying integer object array, flag value is default 59$temp_array = $unsorted_int_obj; 60var_dump(asort($temp_array) ); 61var_dump($temp_array); 62 63// testing asort() function by supplying string object array, flag value is default 64$temp_array = $unsorted_str_obj; 65var_dump(asort($temp_array) ); 66var_dump($temp_array); 67 68echo "\n-- Testing asort() by supplying various object arrays, 'flag' value is SORT_REGULAR --\n"; 69// testing asort() function by supplying integer object array, flag value = SORT_REGULAR 70$temp_array = $unsorted_int_obj; 71var_dump(asort($temp_array, SORT_REGULAR) ); 72var_dump($temp_array); 73 74// testing asort() function by supplying string object array, flag value = SORT_REGULAR 75$temp_array = $unsorted_str_obj; 76var_dump(asort($temp_array, SORT_REGULAR) ); 77var_dump($temp_array); 78 79echo "Done\n"; 80?> 81--EXPECTF-- 82*** Testing asort() : object functionality *** 83 84-- Testing asort() by supplying various object arrays, 'flag' value is default -- 85bool(true) 86array(6) { 87 [4]=> 88 object(for_integer_asort)#%d (1) { 89 ["class_value"]=> 90 int(-5) 91 } 92 [6]=> 93 object(for_integer_asort)#%d (1) { 94 ["class_value"]=> 95 int(0) 96 } 97 [5]=> 98 object(for_integer_asort)#%d (1) { 99 ["class_value"]=> 100 float(0.001) 101 } 102 [1]=> 103 object(for_integer_asort)#%d (1) { 104 ["class_value"]=> 105 int(11) 106 } 107 [3]=> 108 object(for_integer_asort)#%d (1) { 109 ["class_value"]=> 110 int(23) 111 } 112 [2]=> 113 object(for_integer_asort)#%d (1) { 114 ["class_value"]=> 115 int(66) 116 } 117} 118bool(true) 119array(8) { 120 ["g"]=> 121 object(for_string_asort)#%d (1) { 122 ["class_value"]=> 123 string(5) "Lemon" 124 } 125 ["f"]=> 126 object(for_string_asort)#%d (1) { 127 ["class_value"]=> 128 string(6) "Orange" 129 } 130 ["h"]=> 131 object(for_string_asort)#%d (1) { 132 ["class_value"]=> 133 string(5) "aPPle" 134 } 135 ["e"]=> 136 object(for_string_asort)#%d (1) { 137 ["class_value"]=> 138 string(5) "apple" 139 } 140 ["a"]=> 141 object(for_string_asort)#%d (1) { 142 ["class_value"]=> 143 string(3) "axx" 144 } 145 ["d"]=> 146 object(for_string_asort)#%d (1) { 147 ["class_value"]=> 148 string(2) "py" 149 } 150 ["b"]=> 151 object(for_string_asort)#%d (1) { 152 ["class_value"]=> 153 string(1) "t" 154 } 155 ["c"]=> 156 object(for_string_asort)#%d (1) { 157 ["class_value"]=> 158 string(1) "w" 159 } 160} 161 162-- Testing asort() by supplying various object arrays, 'flag' value is SORT_REGULAR -- 163bool(true) 164array(6) { 165 [4]=> 166 object(for_integer_asort)#%d (1) { 167 ["class_value"]=> 168 int(-5) 169 } 170 [6]=> 171 object(for_integer_asort)#%d (1) { 172 ["class_value"]=> 173 int(0) 174 } 175 [5]=> 176 object(for_integer_asort)#%d (1) { 177 ["class_value"]=> 178 float(0.001) 179 } 180 [1]=> 181 object(for_integer_asort)#%d (1) { 182 ["class_value"]=> 183 int(11) 184 } 185 [3]=> 186 object(for_integer_asort)#%d (1) { 187 ["class_value"]=> 188 int(23) 189 } 190 [2]=> 191 object(for_integer_asort)#%d (1) { 192 ["class_value"]=> 193 int(66) 194 } 195} 196bool(true) 197array(8) { 198 ["g"]=> 199 object(for_string_asort)#%d (1) { 200 ["class_value"]=> 201 string(5) "Lemon" 202 } 203 ["f"]=> 204 object(for_string_asort)#%d (1) { 205 ["class_value"]=> 206 string(6) "Orange" 207 } 208 ["h"]=> 209 object(for_string_asort)#%d (1) { 210 ["class_value"]=> 211 string(5) "aPPle" 212 } 213 ["e"]=> 214 object(for_string_asort)#%d (1) { 215 ["class_value"]=> 216 string(5) "apple" 217 } 218 ["a"]=> 219 object(for_string_asort)#%d (1) { 220 ["class_value"]=> 221 string(3) "axx" 222 } 223 ["d"]=> 224 object(for_string_asort)#%d (1) { 225 ["class_value"]=> 226 string(2) "py" 227 } 228 ["b"]=> 229 object(for_string_asort)#%d (1) { 230 ["class_value"]=> 231 string(1) "t" 232 } 233 ["c"]=> 234 object(for_string_asort)#%d (1) { 235 ["class_value"]=> 236 string(1) "w" 237 } 238} 239Done 240