xref: /PHP-7.2/ext/standard/tests/serialize/005.phpt (revision 17ccbeec)
1--TEST--
2serialize()/unserialize() objects
3--SKIPIF--
4<?php if (!interface_exists('Serializable')) die('skip Interface Serialzable not defined'); ?>
5--FILE--
6<?php
7
8// This test verifies that old and new style (un)serializing do not interfere.
9
10function do_autoload($class_name)
11{
12	if ($class_name != 'autoload_not_available')
13	{
14		require_once(dirname(__FILE__) . '/' . strtolower($class_name) . '.p5c');
15	}
16	echo __FUNCTION__ . "($class_name)\n";
17}
18
19function unserializer($class_name)
20{
21	echo __METHOD__ . "($class_name)\n";
22	switch($class_name)
23	{
24	case 'TestNAOld':
25		eval("class TestNAOld extends TestOld {}");
26		break;
27	case 'TestNANew':
28		eval("class TestNANew extends TestNew {}");
29		break;
30	case 'TestNANew2':
31		eval("class TestNANew2 extends TestNew {}");
32		break;
33	default:
34		echo "Try autoloader\n";
35		if (!spl_autoload_functions()) {
36			spl_autoload_register(function ($class_name) { do_autoload($class_name); });
37		}
38		spl_autoload_call($class_name);
39		break;
40	}
41}
42
43ini_set('unserialize_callback_func', 'unserializer');
44
45class TestOld
46{
47	function serialize()
48	{
49		echo __METHOD__ . "()\n";
50	}
51
52	function unserialize($serialized)
53	{
54		echo __METHOD__ . "()\n";
55	}
56
57	function __wakeup()
58	{
59		echo __METHOD__ . "()\n";
60	}
61
62	function __sleep()
63	{
64		echo __METHOD__ . "()\n";
65		return array();
66	}
67}
68
69class TestNew implements Serializable
70{
71	protected static $check = 0;
72
73	function serialize()
74	{
75		echo __METHOD__ . "()\n";
76		switch(++self::$check)
77		{
78		case 1:
79			return NULL;
80		case 2:
81			return "2";
82		}
83	}
84
85	function unserialize($serialized)
86	{
87		echo __METHOD__ . "()\n";
88	}
89
90	function __wakeup()
91	{
92		echo __METHOD__ . "()\n";
93	}
94
95	function __sleep()
96	{
97		echo __METHOD__ . "()\n";
98	}
99}
100
101echo "===O1===\n";
102var_dump($ser = serialize(new TestOld));
103var_dump(unserialize($ser));
104
105echo "===N1===\n";
106var_dump($ser = serialize(new TestNew));
107var_dump(unserialize($ser));
108
109echo "===N2===\n";
110var_dump($ser = serialize(new TestNew));
111var_dump(unserialize($ser));
112
113echo "===NAOld===\n";
114var_dump(unserialize('O:9:"TestNAOld":0:{}'));
115
116echo "===NANew===\n";
117var_dump(unserialize('O:9:"TestNANew":0:{}'));
118
119echo "===NANew2===\n";
120var_dump(unserialize('C:10:"TestNANew2":0:{}'));
121
122echo "===AutoOld===\n";
123var_dump(unserialize('O:19:"autoload_implements":0:{}'));
124
125// Now we have an autoloader, that will be called before the old style header.
126// If the old style handler also fails to register the class then the object
127// becomes an incomplete class instance.
128
129echo "===AutoNA===\n";
130var_dump(unserialize('O:22:"autoload_not_available":0:{}'));
131?>
132===DONE===
133<?php exit(0); ?>
134--EXPECTF--
135===O1===
136TestOld::__sleep()
137string(18) "O:7:"TestOld":0:{}"
138TestOld::__wakeup()
139object(TestOld)#%d (0) {
140}
141===N1===
142TestNew::serialize()
143string(2) "N;"
144NULL
145===N2===
146TestNew::serialize()
147string(19) "C:7:"TestNew":1:{2}"
148TestNew::unserialize()
149object(TestNew)#%d (0) {
150}
151===NAOld===
152unserializer(TestNAOld)
153TestOld::__wakeup()
154object(TestNAOld)#%d (0) {
155}
156===NANew===
157unserializer(TestNANew)
158
159Warning: Erroneous data format for unserializing 'TestNANew' in %s005.php on line %d
160
161Notice: unserialize(): Error at offset 19 of 20 bytes in %s005.php on line %d
162bool(false)
163===NANew2===
164unserializer(TestNANew2)
165TestNew::unserialize()
166object(TestNANew2)#%d (0) {
167}
168===AutoOld===
169unserializer(autoload_implements)
170Try autoloader
171do_autoload(autoload_interface)
172do_autoload(autoload_implements)
173object(autoload_implements)#%d (0) {
174}
175===AutoNA===
176do_autoload(autoload_not_available)
177unserializer(autoload_not_available)
178Try autoloader
179do_autoload(autoload_not_available)
180do_autoload(autoload_not_available)
181
182Warning: unserialize(): Function unserializer() hasn't defined the class it was called for in %s005.php on line %d
183object(__PHP_Incomplete_Class)#%d (1) {
184  ["__PHP_Incomplete_Class_Name"]=>
185  string(22) "autoload_not_available"
186}
187===DONE===
188