array_fill_object.phpt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. --TEST--
  2. Test array_fill() function : usage variations - various object values for 'val' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_fill(int $start_key, int $num, mixed $val)
  6. * Description: Create an array containing num elements starting with index start_key each initialized to val
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * testing array_fill() by passing various object values for 'val' argument
  11. */
  12. echo "*** Testing array_fill() : usage variations ***\n";
  13. // Initialise function arguments not being substituted
  14. $start_key = 0;
  15. $num = 2;
  16. // class without a member
  17. class Test
  18. {
  19. }
  20. //class with public member, static member , constant and consturctor to initialize the public member
  21. class Test1
  22. {
  23. const test1_constant = "test1";
  24. public static $test1_static = 0;
  25. public $member1;
  26. var $var1 = 30;
  27. var $var2;
  28. function __construct($value1 , $value2)
  29. {
  30. $this->member1 = $value1;
  31. $this->var2 = $value2;
  32. }
  33. }
  34. // child class which inherits parent class test1
  35. class Child_test1 extends Test1
  36. {
  37. public $member2;
  38. function __construct($value1 , $value2 , $value3)
  39. {
  40. parent::__construct($value1 , $value2);
  41. $this->member2 = $value3;
  42. }
  43. }
  44. //class with private member, static member, constant and constructor to initialize the private member
  45. class Test2
  46. {
  47. const test2_constant = "test2";
  48. public static $test2_static = 0;
  49. private $member1;
  50. var $var1 = 30;
  51. var $var2;
  52. function __construct($value1 , $value2)
  53. {
  54. $this->member1 = $value1;
  55. $this->var2 = $value2;
  56. }
  57. }
  58. // child class which inherits parent class test2
  59. class Child_test2 extends Test2
  60. {
  61. private $member1;
  62. function __construct($value1 , $value2 , $value3)
  63. {
  64. parent::__construct($value1 , $value2);
  65. $this->member1 = $value3;
  66. }
  67. }
  68. // class with protected member, static member, constant and consturctor to initialize the protected member
  69. class Test3
  70. {
  71. const test3_constant = "test3";
  72. public static $test3_static = 0;
  73. protected $member1;
  74. var $var1 = 30;
  75. var $var2;
  76. function __construct($value1 , $value2)
  77. {
  78. $this->member1 = $value1;
  79. $this->var2 = $value2;
  80. }
  81. }
  82. // child class which inherits parent class test3
  83. class Child_test3 extends Test3
  84. {
  85. protected $member1;
  86. function __construct($value1 , $value2 , $value3)
  87. {
  88. parent::__construct($value1 , $value2);
  89. $this->member1 = $value3;
  90. }
  91. }
  92. // class with public, private, protected members, static, constant members and constructor to initialize all the members
  93. class Test4
  94. {
  95. const test4_constant = "test4";
  96. public static $test4_static = 0;
  97. public $member1;
  98. private $member2;
  99. protected $member3;
  100. function __construct($value1 , $value2 , $value3)
  101. {
  102. $this->member1 = $value1;
  103. $this->member2 = $value2;
  104. $this->member3 = $value3;
  105. }
  106. }
  107. // child class which inherits parent class test4
  108. class Child_test4 extends Test4
  109. {
  110. var $var1;
  111. function __construct($value1 , $value2 , $value3 , $value4)
  112. {
  113. parent::__construct($value1 , $value2 , $value3);
  114. $this->var1 = $value4;
  115. }
  116. }
  117. // abstract class with public, private, protected members
  118. abstract class AbstractClass
  119. {
  120. public $member1;
  121. private $member2;
  122. protected $member3;
  123. var $var1 = 30;
  124. abstract protected function display();
  125. }
  126. // implement abstract 'AbstractClass' class
  127. class ConcreteClass1 extends AbstractClass
  128. {
  129. protected function display()
  130. {
  131. echo "class name is ConcreteClass1 \n";
  132. }
  133. }
  134. // declarationn of the interface 'iTemplate'
  135. interface iTemplate
  136. {
  137. public function display();
  138. }
  139. // implement the interface 'iTemplate'
  140. class Template1 implements iTemplate
  141. {
  142. public function display()
  143. {
  144. echo "class name is Template1\n";
  145. }
  146. }
  147. //array of object values for 'val' argument
  148. $objects = array(
  149. /* 1 */ new Test(),
  150. new Test1(100 , 101),
  151. new Child_test1(100 , 101 , 102),
  152. new Test2(100 , 101),
  153. /* 5 */ new Child_test2(100 , 101 , 102),
  154. new Test3(100 , 101),
  155. new Child_test3(100 , 101 , 102),
  156. new Test4( 100 , 101 , 102),
  157. new Child_test4(100 , 101 , 102 , 103),
  158. new ConcreteClass1(),
  159. /* 11 */ new Template1()
  160. );
  161. // loop through each element of the array for 'val' argument
  162. // check the working of array_fill()
  163. echo "--- Testing array_fill() with different object values for 'val' argument ---\n";
  164. $counter = 1;
  165. for($index = 0; $index < count($objects); $index ++)
  166. {
  167. echo "-- Iteration $counter --\n";
  168. $val = $objects[$index];
  169. var_dump( array_fill($start_key,$num,$val) );
  170. $counter++;
  171. }
  172. echo "Done";
  173. ?>
  174. --EXPECTF--
  175. *** Testing array_fill() : usage variations ***
  176. --- Testing array_fill() with different object values for 'val' argument ---
  177. -- Iteration 1 --
  178. array(2) {
  179. [0]=>
  180. object(Test)#%d (0) {
  181. }
  182. [1]=>
  183. object(Test)#%d (0) {
  184. }
  185. }
  186. -- Iteration 2 --
  187. array(2) {
  188. [0]=>
  189. object(Test1)#%d (3) {
  190. ["member1"]=>
  191. int(100)
  192. ["var1"]=>
  193. int(30)
  194. ["var2"]=>
  195. int(101)
  196. }
  197. [1]=>
  198. object(Test1)#%d (3) {
  199. ["member1"]=>
  200. int(100)
  201. ["var1"]=>
  202. int(30)
  203. ["var2"]=>
  204. int(101)
  205. }
  206. }
  207. -- Iteration 3 --
  208. array(2) {
  209. [0]=>
  210. object(Child_test1)#%d (4) {
  211. ["member2"]=>
  212. int(102)
  213. ["member1"]=>
  214. int(100)
  215. ["var1"]=>
  216. int(30)
  217. ["var2"]=>
  218. int(101)
  219. }
  220. [1]=>
  221. object(Child_test1)#%d (4) {
  222. ["member2"]=>
  223. int(102)
  224. ["member1"]=>
  225. int(100)
  226. ["var1"]=>
  227. int(30)
  228. ["var2"]=>
  229. int(101)
  230. }
  231. }
  232. -- Iteration 4 --
  233. array(2) {
  234. [0]=>
  235. object(Test2)#%d (3) {
  236. ["member1":"Test2":private]=>
  237. int(100)
  238. ["var1"]=>
  239. int(30)
  240. ["var2"]=>
  241. int(101)
  242. }
  243. [1]=>
  244. object(Test2)#%d (3) {
  245. ["member1":"Test2":private]=>
  246. int(100)
  247. ["var1"]=>
  248. int(30)
  249. ["var2"]=>
  250. int(101)
  251. }
  252. }
  253. -- Iteration 5 --
  254. array(2) {
  255. [0]=>
  256. object(Child_test2)#%d (4) {
  257. ["member1":"Child_test2":private]=>
  258. int(102)
  259. ["var1"]=>
  260. int(30)
  261. ["var2"]=>
  262. int(101)
  263. ["member1":"Test2":private]=>
  264. int(100)
  265. }
  266. [1]=>
  267. object(Child_test2)#%d (4) {
  268. ["member1":"Child_test2":private]=>
  269. int(102)
  270. ["var1"]=>
  271. int(30)
  272. ["var2"]=>
  273. int(101)
  274. ["member1":"Test2":private]=>
  275. int(100)
  276. }
  277. }
  278. -- Iteration 6 --
  279. array(2) {
  280. [0]=>
  281. object(Test3)#%d (3) {
  282. ["member1":protected]=>
  283. int(100)
  284. ["var1"]=>
  285. int(30)
  286. ["var2"]=>
  287. int(101)
  288. }
  289. [1]=>
  290. object(Test3)#%d (3) {
  291. ["member1":protected]=>
  292. int(100)
  293. ["var1"]=>
  294. int(30)
  295. ["var2"]=>
  296. int(101)
  297. }
  298. }
  299. -- Iteration 7 --
  300. array(2) {
  301. [0]=>
  302. object(Child_test3)#%d (3) {
  303. ["member1":protected]=>
  304. int(102)
  305. ["var1"]=>
  306. int(30)
  307. ["var2"]=>
  308. int(101)
  309. }
  310. [1]=>
  311. object(Child_test3)#%d (3) {
  312. ["member1":protected]=>
  313. int(102)
  314. ["var1"]=>
  315. int(30)
  316. ["var2"]=>
  317. int(101)
  318. }
  319. }
  320. -- Iteration 8 --
  321. array(2) {
  322. [0]=>
  323. object(Test4)#%d (3) {
  324. ["member1"]=>
  325. int(100)
  326. ["member2":"Test4":private]=>
  327. int(101)
  328. ["member3":protected]=>
  329. int(102)
  330. }
  331. [1]=>
  332. object(Test4)#%d (3) {
  333. ["member1"]=>
  334. int(100)
  335. ["member2":"Test4":private]=>
  336. int(101)
  337. ["member3":protected]=>
  338. int(102)
  339. }
  340. }
  341. -- Iteration 9 --
  342. array(2) {
  343. [0]=>
  344. object(Child_test4)#%d (4) {
  345. ["var1"]=>
  346. int(103)
  347. ["member1"]=>
  348. int(100)
  349. ["member2":"Test4":private]=>
  350. int(101)
  351. ["member3":protected]=>
  352. int(102)
  353. }
  354. [1]=>
  355. object(Child_test4)#%d (4) {
  356. ["var1"]=>
  357. int(103)
  358. ["member1"]=>
  359. int(100)
  360. ["member2":"Test4":private]=>
  361. int(101)
  362. ["member3":protected]=>
  363. int(102)
  364. }
  365. }
  366. -- Iteration 10 --
  367. array(2) {
  368. [0]=>
  369. object(ConcreteClass1)#%d (4) {
  370. ["member1"]=>
  371. NULL
  372. ["member2":"AbstractClass":private]=>
  373. NULL
  374. ["member3":protected]=>
  375. NULL
  376. ["var1"]=>
  377. int(30)
  378. }
  379. [1]=>
  380. object(ConcreteClass1)#%d (4) {
  381. ["member1"]=>
  382. NULL
  383. ["member2":"AbstractClass":private]=>
  384. NULL
  385. ["member3":protected]=>
  386. NULL
  387. ["var1"]=>
  388. int(30)
  389. }
  390. }
  391. -- Iteration 11 --
  392. array(2) {
  393. [0]=>
  394. object(Template1)#%d (0) {
  395. }
  396. [1]=>
  397. object(Template1)#%d (0) {
  398. }
  399. }
  400. Done