1--TEST--
2MySQL PDO: PDOStatement->fetchObject()
3--SKIPIF--
4<?php
5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8$db = MySQLPDOTest::factory();
9
10try {
11    $query = "SELECT '', NULL, \"\" FROM DUAL";
12    $stmt = $db->prepare($query);
13    $ok = @$stmt->execute();
14} catch (PDOException $e) {
15    die("skip: Test cannot be run with SQL mode ANSI");
16}
17if (!$ok)
18    die("skip: Test cannot be run with SQL mode ANSI");
19?>
20--FILE--
21<?php
22require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
23$db = MySQLPDOTest::factory();
24MySQLPDOTest::createTestTable($db);
25
26try {
27
28    $query = "SELECT id, '', NULL, \"\" FROM test ORDER BY id ASC LIMIT 3";
29    $stmt = $db->prepare($query);
30
31    class myclass {
32
33        private $set_calls = 0;
34        protected static $static_set_calls = 0;
35
36        // NOTE: PDO does not care about protected
37        protected $grp;
38
39        // NOTE: PDO does not care about private and calls __construct() after __set()
40        private function __construct($param1, $param2) {
41            printf("myclass::__construct(%s, %s): %d / %d\n",
42                $param1, $param2,
43                self::$static_set_calls, $this->set_calls);
44        }
45
46        // NOTE: PDO will call __set() prior to calling __construct()
47        public function __set($prop, $value) {
48            $this->not_a_magic_one();
49            printf("myclass::__set(%s, -%s-) %d\n",
50                $prop, var_export($value, true), $this->set_calls, self::$static_set_calls);
51            if ("" != $prop)
52                $this->{$prop} = $value;
53        }
54
55        // NOTE: PDO can call regular methods prior to calling __construct()
56        public function not_a_magic_one() {
57            $this->set_calls++;
58            self::$static_set_calls++;
59        }
60
61    }
62    $stmt->execute();
63    $rowno = 0;
64    $rows[] = array();
65    while (is_object($rows[] = $stmt->fetchObject('myclass', array($rowno++, $rowno))))
66        ;
67
68    var_dump($rows[$rowno - 1]);
69
70    try {
71        $stmt->fetchObject('class_does_not_exist');
72    } catch (TypeError $e) {
73        echo $e->getMessage(), "\n";
74    }
75} catch (PDOException $e) {
76    // we should never get here, we use warnings, but never trust a system...
77    printf("[001] %s, [%s} %s\n",
78        $e->getMessage(), $db->errorInfo(), implode(' ', $db->errorInfo()));
79}
80
81print "done!";
82?>
83--CLEAN--
84<?php
85require __DIR__ . '/mysql_pdo_test.inc';
86MySQLPDOTest::dropTestTable();
87?>
88--EXPECTF--
89myclass::__set(id, -'1'-) 1
90myclass::__set(, -''-) 2
91myclass::__set(null, -NULL-) 3
92myclass::__set(, -''-) 4
93myclass::__construct(0, 1): 4 / 4
94myclass::__set(id, -'2'-) 1
95myclass::__set(, -''-) 2
96myclass::__set(null, -NULL-) 3
97myclass::__set(, -''-) 4
98myclass::__construct(1, 2): 8 / 4
99myclass::__set(id, -'3'-) 1
100myclass::__set(, -''-) 2
101myclass::__set(null, -NULL-) 3
102myclass::__set(, -''-) 4
103myclass::__construct(2, 3): 12 / 4
104object(myclass)#%d (4) {
105  ["set_calls":"myclass":private]=>
106  int(4)
107  ["grp":protected]=>
108  NULL
109  ["id"]=>
110  string(1) "3"
111  ["null"]=>
112  NULL
113}
114PDOStatement::fetchObject(): Argument #1 ($class) must be a valid class name, class_does_not_exist given
115done!
116