1--TEST-- 2Test array_unshift() function : passing object for 'var' argument 3--FILE-- 4<?php 5/* 6 * Testing the functionality of array_unshift() by passing 7 * an object to the $var argument 8*/ 9 10echo "*** Testing array_unshift() : Passing object to \$var argument ***\n"; 11 12// simple class with a variable and method 13class SimpleClass 14{ 15 public $var1 = 1; 16 public function fun1() { 17 return $var1; 18 } 19} 20 21// class without members 22class EmptyClass 23{ 24} 25 26// abstract class 27abstract class AbstractClass 28{ 29 protected $var2 = 5; 30 abstract function emptyFunction(); 31} 32 33// class deriving the above abstract class 34class ChildClass extends AbstractClass 35{ 36 private $var3; 37 public function emptyFunction() { 38 echo "defined in child"; 39 } 40} 41 42// class with final method 43class FinalClass 44{ 45 private $var4; 46 final function finalMethod() { 47 echo "This function can't be overloaded"; 48 } 49} 50 51// class with static members 52class StaticClass 53{ 54 static $var5 = 2; 55 public static function staticMethod() { 56 echo "This is a static method"; 57 } 58} 59 60// array to be passed to $array argument 61$array = array('f' => "first", "s" => 'second', 1, 2.222); 62 63// array containing different types of objects as elements 64$vars = array( 65 new SimpleClass(), 66 new EmptyClass(), 67 new ChildClass(), 68 new FinalClass(), 69 new StaticClass() 70); 71 72// loop through the various elements of $arrays to check the functionality of array_unshift 73$iterator = 1; 74foreach($vars as $var) { 75 echo "-- Iteration $iterator --\n"; 76 77 /* with default argument */ 78 // returns element count in the resulting array after arguments are pushed to 79 // beginning of the given array 80 $temp_array = $array; 81 var_dump( array_unshift($temp_array, $var) ); 82 83 // dump the resulting array 84 var_dump($temp_array); 85 86 /* with optional arguments */ 87 // returns element count in the resulting array after arguments are pushed to 88 // beginning of the given array 89 $temp_array = $array; 90 var_dump( array_unshift($temp_array, $var, "hello", 'world') ); 91 92 // dump the resulting array 93 var_dump($temp_array); 94 $iterator++; 95} 96 97echo "Done"; 98?> 99--EXPECTF-- 100*** Testing array_unshift() : Passing object to $var argument *** 101-- Iteration 1 -- 102int(5) 103array(5) { 104 [0]=> 105 object(SimpleClass)#%d (1) { 106 ["var1"]=> 107 int(1) 108 } 109 ["f"]=> 110 string(5) "first" 111 ["s"]=> 112 string(6) "second" 113 [1]=> 114 int(1) 115 [2]=> 116 float(2.222) 117} 118int(7) 119array(7) { 120 [0]=> 121 object(SimpleClass)#%d (1) { 122 ["var1"]=> 123 int(1) 124 } 125 [1]=> 126 string(5) "hello" 127 [2]=> 128 string(5) "world" 129 ["f"]=> 130 string(5) "first" 131 ["s"]=> 132 string(6) "second" 133 [3]=> 134 int(1) 135 [4]=> 136 float(2.222) 137} 138-- Iteration 2 -- 139int(5) 140array(5) { 141 [0]=> 142 object(EmptyClass)#%d (0) { 143 } 144 ["f"]=> 145 string(5) "first" 146 ["s"]=> 147 string(6) "second" 148 [1]=> 149 int(1) 150 [2]=> 151 float(2.222) 152} 153int(7) 154array(7) { 155 [0]=> 156 object(EmptyClass)#%d (0) { 157 } 158 [1]=> 159 string(5) "hello" 160 [2]=> 161 string(5) "world" 162 ["f"]=> 163 string(5) "first" 164 ["s"]=> 165 string(6) "second" 166 [3]=> 167 int(1) 168 [4]=> 169 float(2.222) 170} 171-- Iteration 3 -- 172int(5) 173array(5) { 174 [0]=> 175 object(ChildClass)#%d (2) { 176 ["var2":protected]=> 177 int(5) 178 ["var3":"ChildClass":private]=> 179 NULL 180 } 181 ["f"]=> 182 string(5) "first" 183 ["s"]=> 184 string(6) "second" 185 [1]=> 186 int(1) 187 [2]=> 188 float(2.222) 189} 190int(7) 191array(7) { 192 [0]=> 193 object(ChildClass)#%d (2) { 194 ["var2":protected]=> 195 int(5) 196 ["var3":"ChildClass":private]=> 197 NULL 198 } 199 [1]=> 200 string(5) "hello" 201 [2]=> 202 string(5) "world" 203 ["f"]=> 204 string(5) "first" 205 ["s"]=> 206 string(6) "second" 207 [3]=> 208 int(1) 209 [4]=> 210 float(2.222) 211} 212-- Iteration 4 -- 213int(5) 214array(5) { 215 [0]=> 216 object(FinalClass)#%d (1) { 217 ["var4":"FinalClass":private]=> 218 NULL 219 } 220 ["f"]=> 221 string(5) "first" 222 ["s"]=> 223 string(6) "second" 224 [1]=> 225 int(1) 226 [2]=> 227 float(2.222) 228} 229int(7) 230array(7) { 231 [0]=> 232 object(FinalClass)#%d (1) { 233 ["var4":"FinalClass":private]=> 234 NULL 235 } 236 [1]=> 237 string(5) "hello" 238 [2]=> 239 string(5) "world" 240 ["f"]=> 241 string(5) "first" 242 ["s"]=> 243 string(6) "second" 244 [3]=> 245 int(1) 246 [4]=> 247 float(2.222) 248} 249-- Iteration 5 -- 250int(5) 251array(5) { 252 [0]=> 253 object(StaticClass)#%d (0) { 254 } 255 ["f"]=> 256 string(5) "first" 257 ["s"]=> 258 string(6) "second" 259 [1]=> 260 int(1) 261 [2]=> 262 float(2.222) 263} 264int(7) 265array(7) { 266 [0]=> 267 object(StaticClass)#%d (0) { 268 } 269 [1]=> 270 string(5) "hello" 271 [2]=> 272 string(5) "world" 273 ["f"]=> 274 string(5) "first" 275 ["s"]=> 276 string(6) "second" 277 [3]=> 278 int(1) 279 [4]=> 280 float(2.222) 281} 282Done 283