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