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