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            private static $instance = null;
20            protected $myprotected = 'a protected property';
21
22            // Good old magic stuff
23            private function __construct($caller = NULL) {
24                printf("%s(%s)\n", __METHOD__, $caller);
25            }
26
27
28            public function __destruct() {
29                // printf("%s()\n", __METHOD__);
30            }
31
32            public function __sleep() {
33                printf("%s()\n", __METHOD__);
34            }
35
36            public function __wakeup() {
37                printf("%s()\n", __METHOD__);
38            }
39
40            public function __call($method, $params) {
41                printf("%s(%s, %s)\n", __METHOD__, $method, var_export($params, true));
42            }
43
44            public function __set($prop, $value) {
45                printf("%s(%s, %s)\n", __METHOD__, $prop, var_export($value, true));
46                $this->{$prop} = $value;
47            }
48
49            public function __get($prop) {
50                printf("%s(%s)\n", __METHOD__, $prop);
51                return NULL;
52            }
53
54            // Singleton
55            public static function singleton($caller) {
56                printf("%s(%s)\n", __METHOD__, $caller);
57
58                if (!self::$instance) {
59                    $c = __CLASS__;
60                    self::$instance = new $c($caller);
61                }
62                return self::$instance;
63            }
64
65            // Serializable
66            public function serialize() {
67                printf("%s()\n", __METHOD__);
68                return 'Data from serialize';
69            }
70
71            public function unserialize($data) {
72                printf("%s(%s)\n", __METHOD__, var_export($data, true));
73            }
74
75        }
76
77        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
78        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
79            printf("[002] Unable to turn off emulated prepared statements\n");
80
81        $db->exec('DROP TABLE IF EXISTS test');
82        $db->exec(sprintf('CREATE TABLE test(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(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
97        printf("\nUsing PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...\n");
98        $stmt = $db->prepare('SELECT myobj FROM test');
99        $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('PDO shall not call __construct()'));
100        $stmt->execute();
101        var_dump($stmt->fetch());
102
103        printf("\nUsing PDO::FETCH_CLASS to fetch the object from DB and unserialize it...\n");
104        $stmt = $db->prepare('SELECT myobj FROM test');
105        $stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass', array('PDO shall call __construct()'));
106        $stmt->execute();
107        var_dump($stmt->fetch());
108
109
110    } catch (PDOException $e) {
111        printf("[001] %s [%s] %s\n",
112            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
113    }
114
115    print "done!\n";
116?>
117--CLEAN--
118<?php
119require __DIR__ . '/mysql_pdo_test.inc';
120$db = MySQLPDOTest::factory();
121$db->exec('DROP TABLE IF EXISTS test');
122?>
123--EXPECTF--
124Deprecated: %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
125Creating an object, serializing it and writing it to DB...
126myclass::singleton(Creating object)
127myclass::__construct(Creating object)
128myclass::serialize()
129
130Unserializing the previously serialized object...
131myclass::unserialize('Data from serialize')
132object(myclass)#4 (1) {
133  ["myprotected":protected]=>
134  string(20) "a protected property"
135}
136
137Using PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...
138
139Deprecated: PDOStatement::setFetchMode(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d
140
141Deprecated: PDOStatement::fetch(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d
142myclass::unserialize('C:7:"myclass":19:{Data from serialize}')
143object(myclass)#%d (1) {
144  ["myprotected":protected]=>
145  string(20) "a protected property"
146}
147
148Using PDO::FETCH_CLASS to fetch the object from DB and unserialize it...
149myclass::__set(myobj, 'C:7:"myclass":19:{Data from serialize}')
150myclass::__construct(PDO shall call __construct())
151object(myclass)#%d (2) {
152  ["myprotected":protected]=>
153  string(20) "a protected property"
154  ["myobj"]=>
155  string(38) "C:7:"myclass":19:{Data from serialize}"
156}
157done!
158