1--TEST--
2mysqli_fetch_object()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    include_once("connect.inc");
12
13    set_error_handler('handle_catchable_fatal');
14
15    require('table.inc');
16    if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5")) {
17        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
18    }
19
20    $obj = mysqli_fetch_object($res);
21    if (($obj->ID !== "1") || ($obj->label !== "a") || (get_class($obj) != 'stdClass')) {
22        printf("[004] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
23        var_dump($obj);
24    }
25
26    class mysqli_fetch_object_test {
27        public $ID;
28        public $label;
29        public $a = null;
30        public $b = null;
31
32        public function toString() {
33            var_dump($this);
34        }
35    }
36
37    $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_test');
38    if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
39        printf("[005] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
40        var_dump($obj);
41    }
42
43
44
45    class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
46
47        public function __construct($a, $b) {
48            $this->a = $a;
49            $this->b = $b;
50        }
51
52    }
53
54    try {
55        $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array());
56        if (($obj->ID !== "3") || ($obj->label !== "c") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
57            printf("[006] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58            var_dump($obj);
59        }
60    } catch (Throwable $e) {
61        echo "Exception: " . $e->getMessage() . "\n";
62    }
63
64    try {
65        $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a'));
66        if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
67            printf("[007] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
68            var_dump($obj);
69        }
70    } catch (Throwable $e) {
71        echo "Exception: " . $e->getMessage() . "\n";
72    }
73
74    $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b'));
75    if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
76        printf("[008] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
77        var_dump($obj);
78    }
79
80    var_dump(mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b', 'c')));
81    var_dump(mysqli_fetch_object($res));
82
83    mysqli_free_result($res);
84
85    if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
86        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
87    }
88
89    mysqli_free_result($res);
90    try {
91        mysqli_fetch_object($res);
92    } catch (Error $exception) {
93        echo $exception->getMessage() . "\n";
94    }
95
96    if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
97            printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
98
99    /*
100    TODO
101    I'm using the procedural interface, this should not throw an exception.
102    Also, I did not ask to get exceptions using the mysqli_options()
103    */
104    try {
105        if (false !== ($obj = @mysqli_fetch_object($res, 'mysqli_fetch_object_construct', 'a')))
106            printf("[011] Should have failed\n");
107    } catch (Error $e) {
108        handle_catchable_fatal($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
109    }
110
111    mysqli_free_result($res);
112
113    if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
114        printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
115
116    class mysqli_fetch_object_private_constructor extends mysqli_fetch_object_test {
117
118        private function __construct($a, $b) {
119            $this->a = $a;
120            $this->b = $b;
121        }
122    }
123    /*
124    TODO
125    I think we should bail out here. The following line will give a Fatal error: Call to private ... from invalid context
126    var_dump($obj = new mysqli_fetch_object_private_constructor(1, 2));
127    This does not fail.
128    */
129    $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_private_constructor', array('a', 'b'));
130    mysqli_free_result($res);
131
132    try {
133        var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist'));
134    } catch (TypeError $e) {
135        echo $e->getMessage(), "\n";
136    }
137
138
139    mysqli_close($link);
140    print "done!";
141?>
142--CLEAN--
143<?php
144    require_once("clean_table.inc");
145?>
146--EXPECTF--
147Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 0 passed and exactly 2 expected
148Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected
149NULL
150NULL
151mysqli_result object is already closed
152[0] mysqli_fetch_object(): Argument #3 ($constructor_args) must be of type array, string given in %s on line %d
153mysqli_fetch_object(): Argument #2 ($class) must be a valid class name, this_class_does_not_exist given
154done!
155