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	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array());
63
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
69	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a'));
70	if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
71		printf("[007] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
72		var_dump($obj);
73	}
74
75	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b'));
76	if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
77		printf("[008] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
78		var_dump($obj);
79	}
80
81	var_dump(mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b', 'c')));
82	var_dump(mysqli_fetch_object($res));
83
84	mysqli_free_result($res);
85
86	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
87		printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
88	}
89
90	mysqli_free_result($res);
91	var_dump(mysqli_fetch_object($res));
92
93	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
94			printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
95
96	/*
97	TODO
98	I'm using the procedural interface, this should not throw an exception.
99	Also, I did not ask to get exceptions using the mysqli_options()
100	*/
101	try {
102		if (false !== ($obj = @mysqli_fetch_object($res, 'mysqli_fetch_object_construct', 'a')))
103			printf("[011] Should have failed\n");
104	} catch (Error $e) {
105		handle_catchable_fatal($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
106	}
107
108	mysqli_free_result($res);
109
110	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
111		printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
112
113	class mysqli_fetch_object_private_constructor extends mysqli_fetch_object_test {
114
115		private function __construct($a, $b) {
116			$this->a = $a;
117			$this->b = $b;
118		}
119	}
120	/*
121	TODO
122	I think we should bail out here. The following line will give a Fatal error: Call to private ... from invalid context
123	var_dump($obj = new mysqli_fetch_object_private_constructor(1, 2));
124	This does not fail.
125	*/
126	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_private_constructor', array('a', 'b'));
127	mysqli_free_result($res);
128
129	// Fatal error, script execution will end
130	var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist'));
131
132
133	mysqli_close($link);
134	print "done!";
135?>
136--CLEAN--
137<?php
138	require_once("clean_table.inc");
139?>
140--EXPECTF--
141[E_WARNING] mysqli_fetch_object() expects at least 1 parameter, 0 given in %s on line %d
142[E_WARNING] mysqli_fetch_object() expects parameter 1 to be mysqli_result, null given in %s on line %d
143[E_WARNING] Missing argument 1 for mysqli_fetch_object_construct::__construct() in %s on line %d
144[E_WARNING] Missing argument 2 for mysqli_fetch_object_construct::__construct() in %s on line %d
145[E_NOTICE] Undefined variable: a in %s on line %d
146[E_NOTICE] Undefined variable: b in %s on line %d
147[E_WARNING] Missing argument 2 for mysqli_fetch_object_construct::__construct() in %s on line %d
148[E_NOTICE] Undefined variable: b in %s on line %d
149NULL
150NULL
151[E_WARNING] mysqli_fetch_object(): Couldn't fetch mysqli_result in %s on line %d
152NULL
153[0] Argument 3 passed to mysqli_fetch_object() must be of the type array, string given in %s on line %d
154
155Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d
156