1--TEST-- 2MySQL PDOStatement->fetch(), PDO::FETCH_SERIALIZE 3--SKIPIF-- 4<?php 5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc'); 6require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 7MySQLPDOTest::skip(); 8?> 9--FILE-- 10<?php 11 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 12 $db = MySQLPDOTest::factory(); 13 14 try { 15 16 class myclass implements Serializable { 17 18 public function __construct($caller = null) { 19 printf("%s(%s) - note that it must not be called when unserializing\n", __METHOD__, var_export($caller, true)); 20 } 21 22 public function __set($prop, $value) { 23 printf("%s(%s, %s)\n", __METHOD__, var_export($prop, true), var_export($value, true)); 24 $this->{$prop} = $value; 25 } 26 27 public function serialize() { 28 printf("%s()\n", __METHOD__); 29 return 'Value from serialize()'; 30 } 31 32 public function unserialize($data) { 33 printf("%s(%s)\n", __METHOD__, var_export($data, true)); 34 } 35 36 } 37 38 printf("Lets see what the Serializeable interface makes our object behave like...\n"); 39 $obj = new myclass('Called by script'); 40 $tmp = unserialize(serialize($obj)); 41 var_dump($tmp); 42 43 printf("\nAnd now magic PDO using fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE)...\n"); 44 $db->exec('DROP TABLE IF EXISTS test'); 45 $db->exec(sprintf('CREATE TABLE test(myobj BLOB) ENGINE=%s', MySQLPDOTest::getTableEngine())); 46 $db->exec("INSERT INTO test(myobj) VALUES ('Data fetched from DB to be given to unserialize()')"); 47 48 $stmt = $db->prepare('SELECT myobj FROM test'); 49 $stmt->execute(); 50 $rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('Called by PDO')); 51 var_dump($rows[0]); 52 53 $stmt->execute(); 54 $rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass'); 55 var_dump($rows[0]); 56 57 printf("\nAnd now PDO using setFetchMode(PDO::FETCH:CLASS|PDO::FETCH_SERIALIZE) + fetch()...\n"); 58 $stmt = $db->prepare('SELECT myobj FROM test'); 59 $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('Called by PDO')); 60 $stmt->execute(); 61 var_dump($stmt->fetch()); 62 63 } catch (PDOException $e) { 64 printf("[001] %s [%s] %s\n", 65 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 66 } 67 68 $db->exec('DROP TABLE IF EXISTS test'); 69 print "done!\n"; 70?> 71--EXPECTF-- 72Lets see what the Serializeable interface makes our object behave like... 73myclass::__construct('Called by script') - note that it must not be called when unserializing 74myclass::serialize() 75myclass::unserialize('Value from serialize()') 76object(myclass)#%d (0) { 77} 78 79And now magic PDO using fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE)... 80myclass::unserialize('Data fetched from DB to be given to unserialize()') 81object(myclass)#%d (0) { 82} 83myclass::unserialize('Data fetched from DB to be given to unserialize()') 84object(myclass)#%d (0) { 85} 86 87And now PDO using setFetchMode(PDO::FETCH:CLASS|PDO::FETCH_SERIALIZE) + fetch()... 88myclass::unserialize('Data fetched from DB to be given to unserialize()') 89object(myclass)#%d (0) { 90} 91done! 92