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