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