xref: /PHP-7.2/tests/classes/serialize_001.phpt (revision 5af586be)
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===DONE===
49<?php exit(0); ?>
50--EXPECTF--
51==========
52string(6) "String"
53Test::__construct(String)
54Test::serialize(String)
55Test::unserialize(String)
56object(Test)#%d (1) {
57  ["data"]=>
58  string(6) "String"
59}
60object(Test)#%d (1) {
61  ["data"]=>
62  string(6) "String"
63}
64==========
65NULL
66Test::__construct()
67Test::serialize()
68NULL
69==========
70int(42)
71Test::__construct(42)
72Test::serialize(42)
73Exception: Test::serialize() must return a string or NULL
74==========
75bool(false)
76Test::__construct()
77Test::serialize()
78Exception: Test::serialize() must return a string or NULL
79===DONE===
80