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    $mysqli = $link;
13
14    if (!$res = $mysqli->query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5")) {
15        printf("[003] [%d] %s\n", $mysqli->errno, $mysqli->error);
16    }
17
18    try {
19        if (!is_null($tmp = @$res->fetch_object($link, $link)))
20            printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21    } catch (Throwable $e) {
22        echo $e::class, ': ', $e->getMessage(), "\n";
23    }
24
25
26    try {
27        if (!is_null($tmp = @$res->fetch_object($link, $link, $link)))
28            printf("[006] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
29    } catch (Throwable $e) {
30        echo $e::class, ': ', $e->getMessage(), "\n";
31    }
32
33    $obj = mysqli_fetch_object($res);
34    if (($obj->ID !== "1") || ($obj->label !== "a") || (get_class($obj) != 'stdClass')) {
35        printf("[007] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
36        var_dump($obj);
37    }
38
39    class mysqli_fetch_object_test {
40        public $ID;
41        public $label;
42        public $a = null;
43        public $b = null;
44
45        public function toString() {
46            var_dump($this);
47        }
48    }
49
50    $obj = $res->fetch_object('mysqli_fetch_object_test');
51    if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
52        printf("[008] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
53        var_dump($obj);
54    }
55
56    class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
57
58        public function __construct($a, $b) {
59            $this->a = $a;
60            $this->b = $b;
61        }
62
63    }
64
65    try {
66        $res->fetch_object('mysqli_fetch_object_construct', null);
67    } catch (Throwable $e) {
68        echo $e::class, ': ', $e->getMessage(), "\n";
69        mysqli_fetch_object($res);
70    }
71
72    try {
73        $obj = $res->fetch_object('mysqli_fetch_object_construct', array('a'));
74        if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
75            printf("[010] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
76            var_dump($obj);
77        }
78    } catch (Throwable $e) {
79        echo $e::class, ': ', $e->getMessage(), "\n";
80    }
81
82    $obj = $res->fetch_object('mysqli_fetch_object_construct', array('a', 'b'));
83    if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
84        printf("[011] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
85        var_dump($obj);
86    }
87
88    var_dump($res->fetch_object('mysqli_fetch_object_construct', array('a', 'b', 'c')));
89    var_dump(mysqli_fetch_object($res));
90
91    mysqli_free_result($res);
92
93    if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
94        printf("[012] [%d] %s\n", $mysqli->errno, $mysqli->error);
95    }
96
97    mysqli_free_result($res);
98
99    try {
100        mysqli_fetch_object($res);
101    } catch (Throwable $e) {
102        echo $e::class, ': ', $e->getMessage(), "\n";
103    }
104
105    try {
106        var_dump($res->fetch_object('this_class_does_not_exist'));
107    } catch (Throwable $e) {
108        echo $e::class, ': ', $e->getMessage(), "\n";
109    }
110
111    $mysqli->close();
112    print "done!";
113?>
114--CLEAN--
115<?php
116    require_once 'clean_table.inc';
117?>
118--EXPECT--
119Error: Object of class mysqli could not be converted to string
120ArgumentCountError: mysqli_result::fetch_object() expects at most 2 arguments, 3 given
121TypeError: mysqli_result::fetch_object(): Argument #2 ($constructor_args) must be of type array, null given
122ArgumentCountError: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected
123NULL
124NULL
125Error: mysqli_result object is already closed
126TypeError: mysqli_result::fetch_object(): Argument #1 ($class) must be a valid class name, this_class_does_not_exist given
127done!
128