1--TEST-- 2MySQL PDOStatement->fetch(), PDO::FETCH_SERIALIZE|PDO::FETCH_CLASS 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_fetchserialize_fetch_class(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_fetchserialize_fetch_class(id, myobj) VALUES (?, ?)'); 90 $stmt->bindValue(1, $id); 91 $stmt->bindValue(2, $myobj); 92 $stmt->execute(); 93 94 printf("\nUsing PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...\n"); 95 $stmt = $db->prepare('SELECT myobj FROM test_stmt_fetchserialize_fetch_class'); 96 $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('PDO shall not call __construct()')); 97 $stmt->execute(); 98 var_dump($stmt->fetch()); 99 } catch (PDOException $e) { 100 printf("[001] %s [%s] %s\n", 101 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 102 } 103 104 print "done!\n"; 105?> 106--CLEAN-- 107<?php 108require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 109$db = MySQLPDOTest::factory(); 110$db->exec('DROP TABLE IF EXISTS test_stmt_fetchserialize_fetch_class'); 111?> 112--EXPECTF-- 113Deprecated: %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 114Creating an object, serializing it and writing it to DB... 115myclass::singleton(Creating object) 116myclass::__construct(Creating object) 117myclass::serialize() 118 119Using PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it... 120 121Deprecated: PDOStatement::setFetchMode(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d 122 123Deprecated: PDOStatement::fetch(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d 124myclass::unserialize('C:7:"myclass":19:{Data from serialize}') 125object(myclass)#%d (1) { 126 ["myprotected":protected]=> 127 string(20) "a protected property" 128} 129done! 130