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