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