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