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--EXPECTF-- 131Deprecated: %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 132===O1=== 133TestOld::__sleep() 134string(18) "O:7:"TestOld":0:{}" 135TestOld::__wakeup() 136object(TestOld)#%d (0) { 137} 138===N1=== 139TestNew::serialize() 140string(2) "N;" 141NULL 142===N2=== 143TestNew::serialize() 144string(19) "C:7:"TestNew":1:{2}" 145TestNew::unserialize() 146object(TestNew)#%d (0) { 147} 148===NAOld=== 149unserializer(TestNAOld) 150TestOld::__wakeup() 151object(TestNAOld)#%d (0) { 152} 153===NANew=== 154unserializer(TestNANew) 155 156Deprecated: %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 157 158Warning: Erroneous data format for unserializing 'TestNANew' in %s005.php on line %d 159 160Warning: unserialize(): Error at offset 19 of 20 bytes in %s on line %d 161bool(false) 162===NANew2=== 163unserializer(TestNANew2) 164 165Deprecated: %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 166TestNew::unserialize() 167object(TestNANew2)#%d (0) { 168} 169===AutoOld=== 170unserializer(autoload_implements) 171Try autoloader 172do_autoload(autoload_interface) 173do_autoload(autoload_implements) 174object(autoload_implements)#%d (0) { 175} 176===AutoNA=== 177do_autoload(autoload_not_available) 178unserializer(autoload_not_available) 179Try autoloader 180do_autoload(autoload_not_available) 181do_autoload(autoload_not_available) 182 183Warning: unserialize(): Function unserializer() hasn't defined the class it was called for in %s005.php on line %d 184object(__PHP_Incomplete_Class)#%d (1) { 185 ["__PHP_Incomplete_Class_Name"]=> 186 string(22) "autoload_not_available" 187} 188