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