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	$tmp    = NULL;
14	$link   = NULL;
15
16	if (!is_null($tmp = @mysqli_fetch_object()))
17		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (!is_null($tmp = @mysqli_fetch_object($link)))
20		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	require('table.inc');
23	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5")) {
24		printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
25	}
26
27	$obj = mysqli_fetch_object($res);
28	if (($obj->ID !== "1") || ($obj->label !== "a") || (get_class($obj) != 'stdClass')) {
29		printf("[004] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
30		var_dump($obj);
31	}
32
33	class mysqli_fetch_object_test {
34
35		public $a = null;
36		public $b = null;
37
38		public function toString() {
39			var_dump($this);
40		}
41	}
42
43	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_test');
44	if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
45		printf("[005] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
46		var_dump($obj);
47	}
48
49
50
51	class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
52
53		public function __construct($a, $b) {
54			$this->a = $a;
55			$this->b = $b;
56		}
57
58	}
59
60	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', null);
61
62	if (($obj->ID !== "3") || ($obj->label !== "c") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
63		printf("[006] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
64		var_dump($obj);
65	}
66
67	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a'));
68	if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
69		printf("[007] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
70		var_dump($obj);
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	var_dump(mysqli_fetch_object($res));
90
91	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
92			printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
93
94	/*
95	TODO
96	I'm using the procedural interface, this should not throw an exception.
97	Also, I did not ask to get exceptions using the mysqli_options()
98	*/
99	try {
100		if (false !== ($obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', 'a')))
101			printf("[011] Should have failed\n");
102	} catch (Exception $e) {
103		printf("%s\n", $e->getMessage());
104	}
105
106	mysqli_free_result($res);
107
108	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
109		printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
110
111	class mysqli_fetch_object_private_constructor extends mysqli_fetch_object_test {
112
113		private function __construct($a, $b) {
114			$this->a = $a;
115			$this->b = $b;
116		}
117	}
118	/*
119	TODO
120	I think we should bail out here. The following line will give a Fatal error: Call to private ... from invalid context
121	var_dump($obj = new mysqli_fetch_object_private_constructor(1, 2));
122	This does not fail.
123	*/
124	$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_private_constructor', array('a', 'b'));
125	mysqli_free_result($res);
126
127	// Fatal error, script execution will end
128	var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist'));
129
130
131	mysqli_close($link);
132	print "done!";
133?>
134--CLEAN--
135<?php
136	require_once("clean_table.inc");
137?>
138--EXPECTF--
139Warning: Missing argument 1 for mysqli_fetch_object_construct::__construct() in %s on line %d
140
141Warning: Missing argument 2 for mysqli_fetch_object_construct::__construct() in %s on line %d
142
143Notice: Undefined variable: a in %s on line %d
144
145Notice: Undefined variable: b in %s on line %d
146
147Warning: Missing argument 2 for mysqli_fetch_object_construct::__construct() in %s on line %d
148
149Notice: Undefined variable: b in %s on line %d
150NULL
151NULL
152
153Warning: mysqli_fetch_object(): Couldn't fetch mysqli_result in %s on line %d
154NULL
155Parameter ctor_params must be an array
156
157Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d