1--TEST-- 2Test uasort() function : object functionality - sort diff. objects 3--FILE-- 4<?php 5 6/* 7 * This testcase tests uasort() functionality with different objects 8 * Objects of different classes: 9 * simple class, 10 * child class, 11 * empty class & 12 * static class 13 */ 14 15echo "*** Testing uasort() : object functionality ***\n"; 16 17// comparison function 18function cmp_function($value1, $value2) 19{ 20 if($value1 == $value2) { 21 return 0; 22 } 23 else if($value1 > $value2) { 24 return 1; 25 } 26 else 27 return -1; 28} 29 30 31// Simple class with single member variable 32class SimpleClass 33{ 34 private $int_value; 35 36 public function __construct($value) { 37 $this->int_value = $value; 38 } 39 40} 41 42// Class without any member 43class EmptyClass 44{ 45} 46 47// Class with static member 48class StaticClass 49{ 50 public static $static_value; 51 public function __construct($value) { 52 StaticClass::$static_value = $value; 53 } 54} 55 56// Abstract class 57abstract class AbstractClass 58{ 59 public $pub_value; 60 public abstract function abstractMethod(); 61} 62 63// Child class extending abstract class 64class ChildClass extends AbstractClass 65{ 66 public $child_value = 100; 67 public function abstractMethod() { 68 $pub_value = 5; 69 } 70 public function __construct($value) { 71 $this->child_value = $value; 72 } 73} 74 75// Testing uasort with StaticClass objects as elements of 'array_arg' 76echo "-- Testing uasort() with StaticClass objects --\n"; 77$array_arg = array( 78 0 => new StaticClass(20), 79 1 => new StaticClass(50), 80 2 => new StaticClass(15), 81 3 => new StaticClass(70), 82); 83var_dump( uasort($array_arg, 'cmp_function') ); 84var_dump($array_arg); 85 86// Testing uasort with EmptyClass objects as elements of 'array_arg' 87echo "-- Testing uasort() with EmptyClass objects --\n"; 88$array_arg = array( 89 0 => new EmptyClass(), 90 1 => new EmptyClass(), 91 2 => new EmptyClass(), 92 3 => new EmptyClass(), 93); 94var_dump( uasort($array_arg, 'cmp_function') ); 95var_dump($array_arg); 96 97// Testing uasort with ChildClass objects as elements of 'array_arg' 98echo "-- Testing uasort() with ChildClass objects --\n"; 99$array_arg = array( 100 0 => new ChildClass(20), 101 1 => new ChildClass(500), 102 2 => new ChildClass(15), 103 3 => new ChildClass(700), 104); 105var_dump( uasort($array_arg, 'cmp_function') ); 106var_dump($array_arg); 107 108echo "Done" 109?> 110--EXPECTF-- 111*** Testing uasort() : object functionality *** 112-- Testing uasort() with StaticClass objects -- 113bool(true) 114array(4) { 115 [0]=> 116 object(StaticClass)#%d (0) { 117 } 118 [1]=> 119 object(StaticClass)#%d (0) { 120 } 121 [2]=> 122 object(StaticClass)#%d (0) { 123 } 124 [3]=> 125 object(StaticClass)#%d (0) { 126 } 127} 128-- Testing uasort() with EmptyClass objects -- 129bool(true) 130array(4) { 131 [0]=> 132 object(EmptyClass)#%d (0) { 133 } 134 [1]=> 135 object(EmptyClass)#%d (0) { 136 } 137 [2]=> 138 object(EmptyClass)#%d (0) { 139 } 140 [3]=> 141 object(EmptyClass)#%d (0) { 142 } 143} 144-- Testing uasort() with ChildClass objects -- 145bool(true) 146array(4) { 147 [2]=> 148 object(ChildClass)#%d (2) { 149 ["pub_value"]=> 150 NULL 151 ["child_value"]=> 152 int(15) 153 } 154 [0]=> 155 object(ChildClass)#%d (2) { 156 ["pub_value"]=> 157 NULL 158 ["child_value"]=> 159 int(20) 160 } 161 [1]=> 162 object(ChildClass)#%d (2) { 163 ["pub_value"]=> 164 NULL 165 ["child_value"]=> 166 int(500) 167 } 168 [3]=> 169 object(ChildClass)#%d (2) { 170 ["pub_value"]=> 171 NULL 172 ["child_value"]=> 173 int(700) 174 } 175} 176Done 177