xref: /PHP-5.3/ext/standard/tests/serialize/005.phpt (revision 610c7fbe)
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 __autoload()\n";
35		if (!function_exists('__autoload'))
36		{
37			eval('function __autoload($class_name) { do_autoload($class_name); }');
38		}
39		__autoload($class_name);
40		break;
41	}
42}
43
44ini_set('unserialize_callback_func', 'unserializer');
45
46class TestOld
47{
48	function serialize()
49	{
50		echo __METHOD__ . "()\n";
51	}
52
53	function unserialize($serialized)
54	{
55		echo __METHOD__ . "()\n";
56	}
57
58	function __wakeup()
59	{
60		echo __METHOD__ . "()\n";
61	}
62
63	function __sleep()
64	{
65		echo __METHOD__ . "()\n";
66		return array();
67	}
68}
69
70class TestNew implements Serializable
71{
72	protected static $check = 0;
73
74	function serialize()
75	{
76		echo __METHOD__ . "()\n";
77		switch(++self::$check)
78		{
79		case 1:
80			return NULL;
81		case 2:
82			return "2";
83		}
84	}
85
86	function unserialize($serialized)
87	{
88		echo __METHOD__ . "()\n";
89	}
90
91	function __wakeup()
92	{
93		echo __METHOD__ . "()\n";
94	}
95
96	function __sleep()
97	{
98		echo __METHOD__ . "()\n";
99	}
100}
101
102echo "===O1===\n";
103var_dump($ser = serialize(new TestOld));
104var_dump(unserialize($ser));
105
106echo "===N1===\n";
107var_dump($ser = serialize(new TestNew));
108var_dump(unserialize($ser));
109
110echo "===N2===\n";
111var_dump($ser = serialize(new TestNew));
112var_dump(unserialize($ser));
113
114echo "===NAOld===\n";
115var_dump(unserialize('O:9:"TestNAOld":0:{}'));
116
117echo "===NANew===\n";
118var_dump(unserialize('O:9:"TestNANew":0:{}'));
119
120echo "===NANew2===\n";
121var_dump(unserialize('C:10:"TestNANew2":0:{}'));
122
123echo "===AutoOld===\n";
124var_dump(unserialize('O:19:"autoload_implements":0:{}'));
125
126// Now we have __autoload(), that will be called before the old style header.
127// If the old style handler also fails to register the class then the object
128// becomes an incomplete class instance.
129
130echo "===AutoNA===\n";
131var_dump(unserialize('O:22:"autoload_not_available":0:{}'));
132?>
133===DONE===
134<?php exit(0); ?>
135--EXPECTF--
136===O1===
137TestOld::__sleep()
138string(18) "O:7:"TestOld":0:{}"
139TestOld::__wakeup()
140object(TestOld)#%d (0) {
141}
142===N1===
143TestNew::serialize()
144string(2) "N;"
145NULL
146===N2===
147TestNew::serialize()
148string(19) "C:7:"TestNew":1:{2}"
149TestNew::unserialize()
150object(TestNew)#%d (0) {
151}
152===NAOld===
153unserializer(TestNAOld)
154TestOld::__wakeup()
155object(TestNAOld)#%d (0) {
156}
157===NANew===
158unserializer(TestNANew)
159TestNew::__wakeup()
160object(TestNANew)#%d (0) {
161}
162===NANew2===
163unserializer(TestNANew2)
164TestNew::unserialize()
165object(TestNANew2)#%d (0) {
166}
167===AutoOld===
168unserializer(autoload_implements)
169Try __autoload()
170do_autoload(autoload_interface)
171do_autoload(autoload_implements)
172object(autoload_implements)#%d (0) {
173}
174===AutoNA===
175do_autoload(autoload_not_available)
176unserializer(autoload_not_available)
177Try __autoload()
178do_autoload(autoload_not_available)
179do_autoload(autoload_not_available)
180
181Warning: unserialize(): Function unserializer() hasn't defined the class it was called for in %s005.php on line %d
182object(__PHP_Incomplete_Class)#%d (1) {
183  ["__PHP_Incomplete_Class_Name"]=>
184  string(22) "autoload_not_available"
185}
186===DONE===
187