2 ion\serialize/serializable
8 class test implements Serializable {
10 function serialize() : string {
13 function unserialize(string $data) : void {
18 class recursive implements Serializable {
24 function serialize() : string {
26 return "node:" . ion\serialize($this->r);
31 function unserialize(string $data) : void {
32 if ($data === "leaf") {
35 $this->id = substr($data, 0, 4);
36 $this->r = ion\unserialize(substr($data, 5));
41 class except implements Serializable {
42 function __construct (public $onserialize = false) {}
43 function serialize() : string {
44 if ($this->onserialize)
45 throw new Exception("except on serialize");
48 function unserialize(string $data) : void {
49 throw new Exception("except on unserialize");
56 $s = ion\serialize([$t, $t]);
58 $c = ion\unserialize($s);
60 $tree = new recursive(new recursive(null));
62 $s = ion\serialize($tree);
64 debug_zval_dump(ion\unserialize($s));
67 ion\serialize(new except(true));
68 } catch (Exception $e) {
69 printf("caught %s: %s\n", get_class($e), $e->getMessage());
72 ion\unserialize(ion\serialize(new except()));
73 } catch (Exception $e) {
74 printf("caught %s: %s\n", get_class($e), $e->getMessage());
81 Deprecated: test implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %sserialize/serializable.php on line %d
83 Deprecated: recursive implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %sserialize/serializable.php on line %d
85 Deprecated: except implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %sserialize/serializable.php on line %d
87 [S::test::{{"foobar"}},r::1]
90 object(test)#%d (1) refcount(2){
92 string(6) "foobar" refcount(1)
95 object(test)#%d (1) refcount(2){
97 string(6) "foobar" refcount(1)
100 object(recursive)#%d (2) {
101 ["id":"recursive":private]=>
104 object(recursive)#%d (2) {
105 ["id":"recursive":private]=>
111 S::recursive::{{"node:S::recursive::{{\"leaf\"}}"}}
112 object(recursive)#%d (2) refcount(1){
113 ["id":"recursive":private]=>
114 string(4) "node" refcount(1)
116 object(recursive)#%d (2) refcount(1){
117 ["id":"recursive":private]=>
118 string(4) "leaf" refcount(1)
123 caught Exception: except on serialize
124 caught Exception: except on unserialize