1--TEST-- 2Test usort() function : object functionality - different number of properties 3--FILE-- 4<?php 5/* Prototype : bool usort(array $array_arg, string $cmp_function) 6 * Description: Sort an array by values using a user-defined comparison function 7 * Source code: ext/standard/array.c 8 */ 9 10/* 11 * Pass an array of objects which have a different number of properties 12 * to test behaviour of usort() 13 */ 14 15echo "*** Testing usort() : object functionality ***\n"; 16 17function simple_cmp($value1, $value2) 18{ 19 if($value1 == $value2) { 20 return 0; 21 } 22 else if($value1 > $value2) { 23 return 1; 24 } 25 else 26 return -1; 27} 28 29// comparison function for SimpleClass2 objects which has more than one member 30function multiple_cmp($value1, $value2) 31{ 32 if($value1->getValue() == $value2->getValue()) 33 return 0; 34 else if($value1->getValue() > $value2->getValue()) 35 return 1; 36 else 37 return -1; 38} 39 40// Simple class with single property 41class SimpleClass1 42{ 43 private $int_value; 44 45 public function __construct($value) { 46 $this->int_value = $value; 47 } 48} 49 50// Simple class with more than one property 51class SimpleClass2 52{ 53 private $int_value; 54 protected $float_value; 55 public $string_value; 56 public function __construct($int, $float, $str) { 57 $this->int_value = $int; 58 $this->float_value = $float; 59 $this->string_value = $str; 60 } 61 public function getValue() { 62 return $this->int_value; 63 } 64} 65 66// array of SimpleClass objects with only one property 67$array_arg = array( 680 => new SimpleClass1(10), 691 => new SimpleClass1(1), 702 => new SimpleClass1(100), 713 => new SimpleClass1(50) 72); 73var_dump( usort($array_arg, 'simple_cmp') ); 74var_dump($array_arg); 75 76// array of SimpleClass objects having more than one properties 77$array_arg = array( 780 => new SimpleClass2(2, 3.4, "mango"), 791 => new SimpleClass2(10, 1.2, "apple"), 802 => new SimpleClass2(5, 2.5, "orange"), 81); 82var_dump( usort($array_arg, 'multiple_cmp') ); 83var_dump($array_arg); 84?> 85===DONE=== 86--EXPECTF-- 87*** Testing usort() : object functionality *** 88bool(true) 89array(4) { 90 [0]=> 91 object(SimpleClass1)#%d (1) { 92 ["int_value":"SimpleClass1":private]=> 93 int(1) 94 } 95 [1]=> 96 object(SimpleClass1)#%d (1) { 97 ["int_value":"SimpleClass1":private]=> 98 int(10) 99 } 100 [2]=> 101 object(SimpleClass1)#%d (1) { 102 ["int_value":"SimpleClass1":private]=> 103 int(50) 104 } 105 [3]=> 106 object(SimpleClass1)#%d (1) { 107 ["int_value":"SimpleClass1":private]=> 108 int(100) 109 } 110} 111bool(true) 112array(3) { 113 [0]=> 114 object(SimpleClass2)#%d (3) { 115 ["int_value":"SimpleClass2":private]=> 116 int(2) 117 ["float_value":protected]=> 118 float(3.4) 119 ["string_value"]=> 120 string(5) "mango" 121 } 122 [1]=> 123 object(SimpleClass2)#%d (3) { 124 ["int_value":"SimpleClass2":private]=> 125 int(5) 126 ["float_value":protected]=> 127 float(2.5) 128 ["string_value"]=> 129 string(6) "orange" 130 } 131 [2]=> 132 object(SimpleClass2)#%d (3) { 133 ["int_value":"SimpleClass2":private]=> 134 int(10) 135 ["float_value":protected]=> 136 float(1.2) 137 ["string_value"]=> 138 string(5) "apple" 139 } 140} 141===DONE===