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