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