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        public $ID;
53        public $label;
54        public $a = null;
55        public $b = null;
56
57        public function toString() {
58            var_dump($this);
59        }
60    }
61
62    $obj = $res->fetch_object('mysqli_fetch_object_test');
63    if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
64        printf("[008] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
65        var_dump($obj);
66    }
67
68    class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
69
70        public function __construct($a, $b) {
71            $this->a = $a;
72            $this->b = $b;
73        }
74
75    }
76
77    try {
78        $res->fetch_object('mysqli_fetch_object_construct', null);
79    } catch (TypeError $exception) {
80        echo $exception->getMessage() . "\n";
81        mysqli_fetch_object($res);
82    }
83
84    try {
85        $obj = $res->fetch_object('mysqli_fetch_object_construct', array('a'));
86        if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
87            printf("[010] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
88            var_dump($obj);
89        }
90    } catch (Throwable $e) {
91        echo "Exception: " . $e->getMessage() . "\n";
92    }
93
94    $obj = $res->fetch_object('mysqli_fetch_object_construct', array('a', 'b'));
95    if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
96        printf("[011] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
97        var_dump($obj);
98    }
99
100    var_dump($res->fetch_object('mysqli_fetch_object_construct', array('a', 'b', 'c')));
101    var_dump(mysqli_fetch_object($res));
102
103    mysqli_free_result($res);
104
105    if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
106        printf("[012] [%d] %s\n", $mysqli->errno, $mysqli->error);
107    }
108
109    mysqli_free_result($res);
110
111    try {
112        mysqli_fetch_object($res);
113    } catch (Error $exception) {
114        echo $exception->getMessage() . "\n";
115    }
116
117    try {
118        var_dump($res->fetch_object('this_class_does_not_exist'));
119    } catch (TypeError $exception) {
120        echo $exception->getMessage() . "\n";
121    }
122
123    $mysqli->close();
124    print "done!";
125?>
126--CLEAN--
127<?php
128    require_once("clean_table.inc");
129?>
130--EXPECTF--
131mysqli object is not fully initialized
132[0] Object of class mysqli could not be converted to string in %s on line %d
133[0] mysqli_result::fetch_object() expects at most 2 arguments, 3 given in %s on line %d
134mysqli_result::fetch_object(): Argument #2 ($constructor_args) must be of type array, null given
135Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected
136NULL
137NULL
138mysqli_result object is already closed
139mysqli_result::fetch_object(): Argument #1 ($class) must be a valid class name, this_class_does_not_exist given
140done!
141