array_fill_object.phpt 8.0 KB

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