005.phpt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. --TEST--
  2. serialize()/unserialize() objects
  3. --FILE--
  4. <?php
  5. // This test verifies that old and new style (un)serializing do not interfere.
  6. function do_autoload($class_name)
  7. {
  8. if ($class_name != 'autoload_not_available')
  9. {
  10. require_once(__DIR__ . '/' . strtolower($class_name) . '.inc');
  11. }
  12. echo __FUNCTION__ . "($class_name)\n";
  13. }
  14. function unserializer($class_name)
  15. {
  16. echo __METHOD__ . "($class_name)\n";
  17. switch($class_name)
  18. {
  19. case 'TestNAOld':
  20. eval("class TestNAOld extends TestOld {}");
  21. break;
  22. case 'TestNANew':
  23. eval("class TestNANew extends TestNew {}");
  24. break;
  25. case 'TestNANew2':
  26. eval("class TestNANew2 extends TestNew {}");
  27. break;
  28. default:
  29. echo "Try autoloader\n";
  30. if (!spl_autoload_functions()) {
  31. spl_autoload_register(function ($class_name) { do_autoload($class_name); });
  32. }
  33. spl_autoload_call($class_name);
  34. break;
  35. }
  36. }
  37. ini_set('unserialize_callback_func', 'unserializer');
  38. class TestOld
  39. {
  40. function serialize()
  41. {
  42. echo __METHOD__ . "()\n";
  43. }
  44. function unserialize($serialized)
  45. {
  46. echo __METHOD__ . "()\n";
  47. }
  48. function __wakeup()
  49. {
  50. echo __METHOD__ . "()\n";
  51. }
  52. function __sleep()
  53. {
  54. echo __METHOD__ . "()\n";
  55. return array();
  56. }
  57. }
  58. class TestNew implements Serializable
  59. {
  60. protected static $check = 0;
  61. function serialize()
  62. {
  63. echo __METHOD__ . "()\n";
  64. switch(++self::$check)
  65. {
  66. case 1:
  67. return NULL;
  68. case 2:
  69. return "2";
  70. }
  71. }
  72. function unserialize($serialized)
  73. {
  74. echo __METHOD__ . "()\n";
  75. }
  76. function __wakeup()
  77. {
  78. echo __METHOD__ . "()\n";
  79. }
  80. function __sleep()
  81. {
  82. echo __METHOD__ . "()\n";
  83. }
  84. }
  85. echo "===O1===\n";
  86. var_dump($ser = serialize(new TestOld));
  87. var_dump(unserialize($ser));
  88. echo "===N1===\n";
  89. var_dump($ser = serialize(new TestNew));
  90. var_dump(unserialize($ser));
  91. echo "===N2===\n";
  92. var_dump($ser = serialize(new TestNew));
  93. var_dump(unserialize($ser));
  94. echo "===NAOld===\n";
  95. var_dump(unserialize('O:9:"TestNAOld":0:{}'));
  96. echo "===NANew===\n";
  97. var_dump(unserialize('O:9:"TestNANew":0:{}'));
  98. echo "===NANew2===\n";
  99. var_dump(unserialize('C:10:"TestNANew2":0:{}'));
  100. echo "===AutoOld===\n";
  101. var_dump(unserialize('O:19:"autoload_implements":0:{}'));
  102. // Now we have an autoloader, that will be called before the old style header.
  103. // If the old style handler also fails to register the class then the object
  104. // becomes an incomplete class instance.
  105. echo "===AutoNA===\n";
  106. var_dump(unserialize('O:22:"autoload_not_available":0:{}'));
  107. ?>
  108. --EXPECTF--
  109. Deprecated: %s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
  110. ===O1===
  111. TestOld::__sleep()
  112. string(18) "O:7:"TestOld":0:{}"
  113. TestOld::__wakeup()
  114. object(TestOld)#%d (0) {
  115. }
  116. ===N1===
  117. TestNew::serialize()
  118. string(2) "N;"
  119. NULL
  120. ===N2===
  121. TestNew::serialize()
  122. string(19) "C:7:"TestNew":1:{2}"
  123. TestNew::unserialize()
  124. object(TestNew)#%d (0) {
  125. }
  126. ===NAOld===
  127. unserializer(TestNAOld)
  128. TestOld::__wakeup()
  129. object(TestNAOld)#%d (0) {
  130. }
  131. ===NANew===
  132. unserializer(TestNANew)
  133. Deprecated: %s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
  134. Warning: Erroneous data format for unserializing 'TestNANew' in %s005.php on line %d
  135. Notice: unserialize(): Error at offset 19 of 20 bytes in %s005.php on line %d
  136. bool(false)
  137. ===NANew2===
  138. unserializer(TestNANew2)
  139. Deprecated: %s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
  140. TestNew::unserialize()
  141. object(TestNANew2)#%d (0) {
  142. }
  143. ===AutoOld===
  144. unserializer(autoload_implements)
  145. Try autoloader
  146. do_autoload(autoload_interface)
  147. do_autoload(autoload_implements)
  148. object(autoload_implements)#%d (0) {
  149. }
  150. ===AutoNA===
  151. do_autoload(autoload_not_available)
  152. unserializer(autoload_not_available)
  153. Try autoloader
  154. do_autoload(autoload_not_available)
  155. do_autoload(autoload_not_available)
  156. Warning: unserialize(): Function unserializer() hasn't defined the class it was called for in %s005.php on line %d
  157. object(__PHP_Incomplete_Class)#%d (1) {
  158. ["__PHP_Incomplete_Class_Name"]=>
  159. string(22) "autoload_not_available"
  160. }