serialize_001.phpt 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. --EXPECTF--
  42. 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
  43. ==========
  44. string(6) "String"
  45. Test::__construct(String)
  46. Test::serialize(String)
  47. Test::unserialize(String)
  48. object(Test)#%d (1) {
  49. ["data"]=>
  50. string(6) "String"
  51. }
  52. object(Test)#%d (1) {
  53. ["data"]=>
  54. string(6) "String"
  55. }
  56. ==========
  57. NULL
  58. Test::__construct()
  59. Test::serialize()
  60. NULL
  61. ==========
  62. int(42)
  63. Test::__construct(42)
  64. Test::serialize(42)
  65. Exception: Test::serialize() must return a string or NULL
  66. ==========
  67. bool(false)
  68. Test::__construct()
  69. Test::serialize()
  70. Exception: Test::serialize() must return a string or NULL