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