Return to 005.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / serialize |
1.1 misho 1: --TEST-- 2: serialize()/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: 10: function 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: 19: function 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: 44: ini_set('unserialize_callback_func', 'unserializer'); 45: 46: class 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: 70: class 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: 102: echo "===O1===\n"; 103: var_dump($ser = serialize(new TestOld)); 104: var_dump(unserialize($ser)); 105: 106: echo "===N1===\n"; 107: var_dump($ser = serialize(new TestNew)); 108: var_dump(unserialize($ser)); 109: 110: echo "===N2===\n"; 111: var_dump($ser = serialize(new TestNew)); 112: var_dump(unserialize($ser)); 113: 114: echo "===NAOld===\n"; 115: var_dump(unserialize('O:9:"TestNAOld":0:{}')); 116: 117: echo "===NANew===\n"; 118: var_dump(unserialize('O:9:"TestNANew":0:{}')); 119: 120: echo "===NANew2===\n"; 121: var_dump(unserialize('C:10:"TestNANew2":0:{}')); 122: 123: echo "===AutoOld===\n"; 124: var_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: 130: echo "===AutoNA===\n"; 131: var_dump(unserialize('O:22:"autoload_not_available":0:{}')); 132: ?> 133: ===DONE=== 134: <?php exit(0); ?> 135: --EXPECTF-- 136: ===O1=== 137: TestOld::__sleep() 138: string(18) "O:7:"TestOld":0:{}" 139: TestOld::__wakeup() 140: object(TestOld)#%d (0) { 141: } 142: ===N1=== 143: TestNew::serialize() 144: string(2) "N;" 145: NULL 146: ===N2=== 147: TestNew::serialize() 148: string(19) "C:7:"TestNew":1:{2}" 149: TestNew::unserialize() 150: object(TestNew)#%d (0) { 151: } 152: ===NAOld=== 153: unserializer(TestNAOld) 154: TestOld::__wakeup() 155: object(TestNAOld)#%d (0) { 156: } 157: ===NANew=== 158: unserializer(TestNANew) 159: TestNew::__wakeup() 160: object(TestNANew)#%d (0) { 161: } 162: ===NANew2=== 163: unserializer(TestNANew2) 164: TestNew::unserialize() 165: object(TestNANew2)#%d (0) { 166: } 167: ===AutoOld=== 168: unserializer(autoload_implements) 169: Try __autoload() 170: do_autoload(autoload_interface) 171: do_autoload(autoload_implements) 172: object(autoload_implements)#%d (0) { 173: } 174: ===AutoNA=== 175: do_autoload(autoload_not_available) 176: unserializer(autoload_not_available) 177: Try __autoload() 178: do_autoload(autoload_not_available) 179: do_autoload(autoload_not_available) 180: 181: Warning: unserialize(): Function unserializer() hasn't defined the class it was called for in %s005.php on line %d 182: object(__PHP_Incomplete_Class)#%d (1) { 183: ["__PHP_Incomplete_Class_Name"]=> 184: string(22) "autoload_not_available" 185: } 186: ===DONE===