array_unshift_object.phpt 4.5 KB

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