1--TEST-- 2MySQL PDOStatement->fetch(), PDO::FETCH_SERIALIZE 3--EXTENSIONS-- 4pdo_mysql 5--SKIPIF-- 6<?php 7require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 8MySQLPDOTest::skip(); 9?> 10--FILE-- 11<?php 12 require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 13 $db = MySQLPDOTest::factory(); 14 15 try { 16 17 #[AllowDynamicProperties] 18 class myclass implements Serializable { 19 20 private static $instance = null; 21 protected $myprotected = 'a protected property'; 22 23 // Good old magic stuff 24 private function __construct($caller = NULL) { 25 printf("%s(%s)\n", __METHOD__, $caller); 26 } 27 28 29 public function __destruct() { 30 // printf("%s()\n", __METHOD__); 31 } 32 33 public function __sleep() { 34 printf("%s()\n", __METHOD__); 35 } 36 37 public function __wakeup() { 38 printf("%s()\n", __METHOD__); 39 } 40 41 public function __call($method, $params) { 42 printf("%s(%s, %s)\n", __METHOD__, $method, var_export($params, true)); 43 } 44 45 public function __set($prop, $value) { 46 printf("%s(%s, %s)\n", __METHOD__, $prop, var_export($value, true)); 47 $this->{$prop} = $value; 48 } 49 50 public function __get($prop) { 51 printf("%s(%s)\n", __METHOD__, $prop); 52 return NULL; 53 } 54 55 // Singleton 56 public static function singleton($caller) { 57 printf("%s(%s)\n", __METHOD__, $caller); 58 59 if (!self::$instance) { 60 $c = __CLASS__; 61 self::$instance = new $c($caller); 62 } 63 return self::$instance; 64 } 65 66 // Serializable 67 public function serialize() { 68 printf("%s()\n", __METHOD__); 69 return 'Data from serialize'; 70 } 71 72 public function unserialize($data) { 73 printf("%s(%s)\n", __METHOD__, var_export($data, true)); 74 } 75 76 } 77 78 $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0); 79 if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 80 printf("[002] Unable to turn off emulated prepared statements\n"); 81 82 $db->exec(sprintf('CREATE TABLE test_stmt_fetch_serialize(id INT, myobj BLOB) ENGINE=%s', 83 MySQLPDOTest::getTableEngine())); 84 85 printf("Creating an object, serializing it and writing it to DB...\n"); 86 $id = 1; 87 $obj = myclass::singleton('Creating object'); 88 $myobj = serialize($obj); 89 $stmt = $db->prepare('INSERT INTO test_stmt_fetch_serialize(id, myobj) VALUES (?, ?)'); 90 $stmt->bindValue(1, $id); 91 $stmt->bindValue(2, $myobj); 92 $stmt->execute(); 93 94 printf("\nUnserializing the previously serialized object...\n"); 95 var_dump(unserialize($myobj)); 96 } catch (PDOException $e) { 97 printf("[001] %s [%s] %s\n", 98 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 99 } 100 101 print "done!\n"; 102?> 103--CLEAN-- 104<?php 105require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 106$db = MySQLPDOTest::factory(); 107$db->exec('DROP TABLE IF EXISTS test_stmt_fetch_serialize'); 108?> 109--EXPECTF-- 110Deprecated: %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 111Creating an object, serializing it and writing it to DB... 112myclass::singleton(Creating object) 113myclass::__construct(Creating object) 114myclass::serialize() 115 116Unserializing the previously serialized object... 117myclass::unserialize('Data from serialize') 118object(myclass)#4 (1) { 119 ["myprotected":protected]=> 120 string(20) "a protected property" 121} 122done! 123