observer_006.phpt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. --TEST--
  2. SPL: SplObjectStorage with accociatied information
  3. --FILE--
  4. <?php
  5. class TestClass
  6. {
  7. public $test = 25;
  8. public function __construct($test = 42)
  9. {
  10. $this->test = $test;
  11. }
  12. }
  13. class MyStorage extends SplObjectStorage
  14. {
  15. public $bla = 25;
  16. public function __construct($bla = 26)
  17. {
  18. $this->bla = $bla;
  19. }
  20. }
  21. $storage = new MyStorage();
  22. foreach(array(1=>"foo",2=>42) as $key => $value)
  23. {
  24. $storage->attach(new TestClass($key), $value);
  25. }
  26. var_dump(count($storage));
  27. foreach($storage as $object)
  28. {
  29. var_dump($object->test);
  30. }
  31. var_dump($storage);
  32. var_dump(serialize($storage));
  33. echo "===UNSERIALIZE===\n";
  34. $storage2 = unserialize(serialize($storage));
  35. var_dump(count($storage2));
  36. foreach($storage2 as $object)
  37. {
  38. var_dump($object->test);
  39. }
  40. var_dump($storage2);
  41. $storage->attach(new TestClass(3), new stdClass);
  42. $storage->attach(new TestClass(4), new TestClass(5));
  43. echo "===UNSERIALIZE2===\n";
  44. var_dump(unserialize(serialize($storage)));
  45. $storage->rewind();
  46. $storage->next();
  47. var_dump($storage->key());
  48. var_dump($storage->current());
  49. var_dump($storage->getInfo());
  50. $storage->setInfo("bar");
  51. var_dump($storage->getInfo());
  52. echo "===UNSERIALIZE3===\n";
  53. var_dump(unserialize(serialize($storage)));
  54. $storage->rewind();
  55. $storage->next();
  56. $storage->next();
  57. var_dump($storage->key());
  58. var_dump($storage->current());
  59. $storage->attach($storage->current(), "replaced");
  60. echo "===UNSERIALIZE4===\n";
  61. var_dump(unserialize(serialize($storage)));
  62. ?>
  63. --EXPECTF--
  64. int(2)
  65. int(1)
  66. int(2)
  67. object(MyStorage)#%d (2) {
  68. ["bla"]=>
  69. int(26)
  70. ["storage":"SplObjectStorage":private]=>
  71. array(2) {
  72. [0]=>
  73. array(2) {
  74. ["obj"]=>
  75. object(TestClass)#%d (1) {
  76. ["test"]=>
  77. int(1)
  78. }
  79. ["inf"]=>
  80. string(3) "foo"
  81. }
  82. [1]=>
  83. array(2) {
  84. ["obj"]=>
  85. object(TestClass)#%d (1) {
  86. ["test"]=>
  87. int(2)
  88. }
  89. ["inf"]=>
  90. int(42)
  91. }
  92. }
  93. }
  94. string(%d) "%s"
  95. ===UNSERIALIZE===
  96. int(2)
  97. int(1)
  98. int(2)
  99. object(MyStorage)#%d (2) {
  100. ["bla"]=>
  101. int(26)
  102. ["storage":"SplObjectStorage":private]=>
  103. array(2) {
  104. [0]=>
  105. array(2) {
  106. ["obj"]=>
  107. object(TestClass)#%d (1) {
  108. ["test"]=>
  109. int(1)
  110. }
  111. ["inf"]=>
  112. string(3) "foo"
  113. }
  114. [1]=>
  115. array(2) {
  116. ["obj"]=>
  117. object(TestClass)#%d (1) {
  118. ["test"]=>
  119. int(2)
  120. }
  121. ["inf"]=>
  122. int(42)
  123. }
  124. }
  125. }
  126. ===UNSERIALIZE2===
  127. object(MyStorage)#%d (2) {
  128. ["bla"]=>
  129. int(26)
  130. ["storage":"SplObjectStorage":private]=>
  131. array(4) {
  132. [0]=>
  133. array(2) {
  134. ["obj"]=>
  135. object(TestClass)#%d (1) {
  136. ["test"]=>
  137. int(1)
  138. }
  139. ["inf"]=>
  140. string(3) "foo"
  141. }
  142. [1]=>
  143. array(2) {
  144. ["obj"]=>
  145. object(TestClass)#%d (1) {
  146. ["test"]=>
  147. int(2)
  148. }
  149. ["inf"]=>
  150. int(42)
  151. }
  152. [2]=>
  153. array(2) {
  154. ["obj"]=>
  155. object(TestClass)#%d (1) {
  156. ["test"]=>
  157. int(3)
  158. }
  159. ["inf"]=>
  160. object(stdClass)#%d (0) {
  161. }
  162. }
  163. [3]=>
  164. array(2) {
  165. ["obj"]=>
  166. object(TestClass)#%d (1) {
  167. ["test"]=>
  168. int(4)
  169. }
  170. ["inf"]=>
  171. object(TestClass)#%d (1) {
  172. ["test"]=>
  173. int(5)
  174. }
  175. }
  176. }
  177. }
  178. int(1)
  179. object(TestClass)#%d (1) {
  180. ["test"]=>
  181. int(2)
  182. }
  183. int(42)
  184. string(3) "bar"
  185. ===UNSERIALIZE3===
  186. object(MyStorage)#%d (2) {
  187. ["bla"]=>
  188. int(26)
  189. ["storage":"SplObjectStorage":private]=>
  190. array(4) {
  191. [0]=>
  192. array(2) {
  193. ["obj"]=>
  194. object(TestClass)#%d (1) {
  195. ["test"]=>
  196. int(1)
  197. }
  198. ["inf"]=>
  199. string(3) "foo"
  200. }
  201. [1]=>
  202. array(2) {
  203. ["obj"]=>
  204. object(TestClass)#%d (1) {
  205. ["test"]=>
  206. int(2)
  207. }
  208. ["inf"]=>
  209. string(3) "bar"
  210. }
  211. [2]=>
  212. array(2) {
  213. ["obj"]=>
  214. object(TestClass)#%d (1) {
  215. ["test"]=>
  216. int(3)
  217. }
  218. ["inf"]=>
  219. object(stdClass)#%d (0) {
  220. }
  221. }
  222. [3]=>
  223. array(2) {
  224. ["obj"]=>
  225. object(TestClass)#%d (1) {
  226. ["test"]=>
  227. int(4)
  228. }
  229. ["inf"]=>
  230. object(TestClass)#%d (1) {
  231. ["test"]=>
  232. int(5)
  233. }
  234. }
  235. }
  236. }
  237. int(2)
  238. object(TestClass)#7 (1) {
  239. ["test"]=>
  240. int(3)
  241. }
  242. ===UNSERIALIZE4===
  243. object(MyStorage)#%d (2) {
  244. ["bla"]=>
  245. int(26)
  246. ["storage":"SplObjectStorage":private]=>
  247. array(4) {
  248. [0]=>
  249. array(2) {
  250. ["obj"]=>
  251. object(TestClass)#%d (1) {
  252. ["test"]=>
  253. int(1)
  254. }
  255. ["inf"]=>
  256. string(3) "foo"
  257. }
  258. [1]=>
  259. array(2) {
  260. ["obj"]=>
  261. object(TestClass)#%d (1) {
  262. ["test"]=>
  263. int(2)
  264. }
  265. ["inf"]=>
  266. string(3) "bar"
  267. }
  268. [2]=>
  269. array(2) {
  270. ["obj"]=>
  271. object(TestClass)#%d (1) {
  272. ["test"]=>
  273. int(3)
  274. }
  275. ["inf"]=>
  276. string(8) "replaced"
  277. }
  278. [3]=>
  279. array(2) {
  280. ["obj"]=>
  281. object(TestClass)#%d (1) {
  282. ["test"]=>
  283. int(4)
  284. }
  285. ["inf"]=>
  286. object(TestClass)#%d (1) {
  287. ["test"]=>
  288. int(5)
  289. }
  290. }
  291. }
  292. }