simplify Serializable handling
[awesomized/ext-ion] / tests / serialize / serializable.phpt
1 --TEST--
2 ion\serialize/serializable
3 --EXTENSIONS--
4 ion
5 --FILE--
6 TEST
7 <?php
8 class test implements Serializable {
9 protected $data;
10 function serialize() : string {
11 return "foobar";
12 }
13 function unserialize(string $data) : void {
14 $this->data = $data;
15 }
16 }
17
18 class recursive implements Serializable {
19 private $id;
20 function __construct(
21 protected $r
22 ) {
23 }
24 function serialize() : string {
25 if ($this->r) {
26 return "node:" . ion\serialize($this->r);
27 } else {
28 return "leaf";
29 }
30 }
31 function unserialize(string $data) : void {
32 if ($data === "leaf") {
33 $this->id = $data;
34 } else {
35 $this->id = substr($data, 0, 4);
36 $this->r = ion\unserialize(substr($data, 5));
37 }
38 }
39 }
40
41 echo "\n";
42
43 $t = new test;
44 $s = ion\serialize([$t, $t]);
45 echo $s, "\n";
46 $c = ion\unserialize($s);
47 debug_zval_dump($c);
48 $tree = new recursive(new recursive(null));
49 var_dump($tree);
50 $s = ion\serialize($tree);
51 echo $s,"\n";
52 debug_zval_dump(ion\unserialize($s));
53 ?>
54 DONE
55 --EXPECTF--
56 TEST
57
58 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
59
60 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
61
62 PHP::[S::test::"foobar",r::1]
63 array(2) refcount(2){
64 [0]=>
65 object(test)#%d (1) refcount(2){
66 ["data":protected]=>
67 string(6) "foobar" refcount(1)
68 }
69 [1]=>
70 object(test)#%d (1) refcount(2){
71 ["data":protected]=>
72 string(6) "foobar" refcount(1)
73 }
74 }
75 object(recursive)#%d (2) {
76 ["id":"recursive":private]=>
77 NULL
78 ["r":protected]=>
79 object(recursive)#%d (2) {
80 ["id":"recursive":private]=>
81 NULL
82 ["r":protected]=>
83 NULL
84 }
85 }
86 PHP::S::recursive::"node:S::recursive::\"leaf\""
87 object(recursive)#%d (2) refcount(1){
88 ["id":"recursive":private]=>
89 string(4) "node" refcount(1)
90 ["r":protected]=>
91 object(recursive)#10 (2) refcount(1){
92 ["id":"recursive":private]=>
93 string(4) "leaf" refcount(1)
94 ["r":protected]=>
95 NULL
96 }
97 }
98 DONE