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