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        #[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('DROP TABLE IF EXISTS test');
83        $db->exec(sprintf('CREATE TABLE test(id INT, myobj BLOB) ENGINE=%s',
84            MySQLPDOTest::getTableEngine()));
85
86        printf("Creating an object, serializing it and writing it to DB...\n");
87        $id = 1;
88        $obj = myclass::singleton('Creating object');
89        $myobj = serialize($obj);
90        $stmt = $db->prepare('INSERT INTO test(id, myobj) VALUES (?, ?)');
91        $stmt->bindValue(1, $id);
92        $stmt->bindValue(2, $myobj);
93        $stmt->execute();
94
95        printf("\nUnserializing the previously serialized object...\n");
96        var_dump(unserialize($myobj));
97
98        printf("\nUsing PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...\n");
99        $stmt = $db->prepare('SELECT myobj FROM test');
100        $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('PDO shall not call __construct()'));
101        $stmt->execute();
102        var_dump($stmt->fetch());
103
104        printf("\nUsing PDO::FETCH_CLASS to fetch the object from DB and unserialize it...\n");
105        $stmt = $db->prepare('SELECT myobj FROM test');
106        $stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass', array('PDO shall call __construct()'));
107        $stmt->execute();
108        var_dump($stmt->fetch());
109
110
111    } catch (PDOException $e) {
112        printf("[001] %s [%s] %s\n",
113            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
114    }
115
116    print "done!\n";
117?>
118--CLEAN--
119<?php
120require __DIR__ . '/mysql_pdo_test.inc';
121$db = MySQLPDOTest::factory();
122$db->exec('DROP TABLE IF EXISTS test');
123?>
124--EXPECTF--
125Deprecated: %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
126Creating an object, serializing it and writing it to DB...
127myclass::singleton(Creating object)
128myclass::__construct(Creating object)
129myclass::serialize()
130
131Unserializing the previously serialized object...
132myclass::unserialize('Data from serialize')
133object(myclass)#4 (1) {
134  ["myprotected":protected]=>
135  string(20) "a protected property"
136}
137
138Using PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...
139
140Deprecated: PDOStatement::setFetchMode(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d
141
142Deprecated: PDOStatement::fetch(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d
143myclass::unserialize('C:7:"myclass":19:{Data from serialize}')
144object(myclass)#%d (1) {
145  ["myprotected":protected]=>
146  string(20) "a protected property"
147}
148
149Using PDO::FETCH_CLASS to fetch the object from DB and unserialize it...
150myclass::__set(myobj, 'C:7:"myclass":19:{Data from serialize}')
151myclass::__construct(PDO shall call __construct())
152object(myclass)#%d (2) {
153  ["myprotected":protected]=>
154  string(20) "a protected property"
155  ["myobj"]=>
156  string(38) "C:7:"myclass":19:{Data from serialize}"
157}
158done!
159