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