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