serialize_001.phpt 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. --TEST--
  2. ZE2 Serializable
  3. --FILE--
  4. <?php
  5. class Test implements Serializable
  6. {
  7. public $data;
  8. function __construct($data)
  9. {
  10. echo __METHOD__ . "($data)\n";
  11. $this->data = $data;
  12. }
  13. function serialize()
  14. {
  15. echo __METHOD__ . "({$this->data})\n";
  16. return $this->data;
  17. }
  18. function unserialize($serialized)
  19. {
  20. echo __METHOD__ . "($serialized)\n";
  21. $this->data = $serialized;
  22. var_dump($this);
  23. }
  24. }
  25. $tests = array('String', NULL, 42, false);
  26. foreach($tests as $data)
  27. {
  28. try
  29. {
  30. echo "==========\n";
  31. var_dump($data);
  32. $ser = serialize(new Test($data));
  33. var_dump(unserialize($ser));
  34. }
  35. catch(Exception $e)
  36. {
  37. echo 'Exception: ' . $e->getMessage() . "\n";
  38. }
  39. }
  40. ?>
  41. ===DONE===
  42. <?php exit(0); ?>
  43. --EXPECTF--
  44. ==========
  45. string(6) "String"
  46. Test::__construct(String)
  47. Test::serialize(String)
  48. Test::unserialize(String)
  49. object(Test)#%d (1) {
  50. ["data"]=>
  51. string(6) "String"
  52. }
  53. object(Test)#%d (1) {
  54. ["data"]=>
  55. string(6) "String"
  56. }
  57. ==========
  58. NULL
  59. Test::__construct()
  60. Test::serialize()
  61. NULL
  62. ==========
  63. int(42)
  64. Test::__construct(42)
  65. Test::serialize(42)
  66. Exception: Test::serialize() must return a string or NULL
  67. ==========
  68. bool(false)
  69. Test::__construct()
  70. Test::serialize()
  71. Exception: Test::serialize() must return a string or NULL
  72. ===DONE===