1--TEST--
2MySQL PDO: PDOStatement->fetchObject()
3--SKIPIF--
4<?php
5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8$db = MySQLPDOTest::factory();
9
10try {
11	$query = "SELECT '', NULL, \"\" FROM DUAL";
12	$stmt = $db->prepare($query);
13	$ok = @$stmt->execute();
14} catch (PDOException $e) {
15	die("skip: Test cannot be run with SQL mode ANSI");
16}
17if (!$ok)
18	die("skip: Test cannot be run with SQL mode ANSI");
19?>
20--FILE--
21<?php
22require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
23$db = MySQLPDOTest::factory();
24MySQLPDOTest::createTestTable($db);
25
26try {
27
28	$query = "SELECT id, '', NULL, \"\" FROM test ORDER BY id ASC LIMIT 3";
29	$stmt = $db->prepare($query);
30
31	class myclass {
32
33		private $set_calls = 0;
34		protected static $static_set_calls = 0;
35
36		// NOTE: PDO does not care about protected
37		protected $grp;
38
39		// NOTE: PDO does not care about private and calls __construct() after __set()
40		private function __construct($param1, $param2) {
41			printf("myclass::__construct(%s, %s): %d / %d\n",
42				$param1, $param2,
43				self::$static_set_calls, $this->set_calls);
44		}
45
46		// NOTE: PDO will call __set() prior to calling __construct()
47		public function __set($prop, $value) {
48			$this->not_a_magic_one();
49			printf("myclass::__set(%s, -%s-) %d\n",
50				$prop, var_export($value, true), $this->set_calls, self::$static_set_calls);
51			if ("" != $prop)
52				$this->{$prop} = $value;
53		}
54
55		// NOTE: PDO can call regular methods prior to calling __construct()
56		public function not_a_magic_one() {
57			$this->set_calls++;
58			self::$static_set_calls++;
59		}
60
61	}
62	$stmt->execute();
63	$rowno = 0;
64	$rows[] = array();
65	while (is_object($rows[] = $stmt->fetchObject('myclass', array($rowno++, $rowno))))
66		;
67
68	var_dump($rows[$rowno - 1]);
69
70} catch (PDOException $e) {
71	// we should never get here, we use warnings, but never trust a system...
72	printf("[001] %s, [%s} %s\n",
73		$e->getMessage(), $db->errorInfo(), implode(' ', $db->errorInfo()));
74}
75
76print "done!";
77?>
78--CLEAN--
79<?php
80require dirname(__FILE__) . '/mysql_pdo_test.inc';
81MySQLPDOTest::dropTestTable();
82?>
83--EXPECTF--
84myclass::__set(id, -'1'-) 1
85myclass::__set(, -''-) 2
86myclass::__set(null, -NULL-) 3
87myclass::__set(, -''-) 4
88myclass::__construct(0, 1): 4 / 4
89myclass::__set(id, -'2'-) 1
90myclass::__set(, -''-) 2
91myclass::__set(null, -NULL-) 3
92myclass::__set(, -''-) 4
93myclass::__construct(1, 2): 8 / 4
94myclass::__set(id, -'3'-) 1
95myclass::__set(, -''-) 2
96myclass::__set(null, -NULL-) 3
97myclass::__set(, -''-) 4
98myclass::__construct(2, 3): 12 / 4
99object(myclass)#%d (4) {
100  ["set_calls":"myclass":private]=>
101  int(4)
102  ["grp":protected]=>
103  NULL
104  ["id"]=>
105  string(1) "3"
106  ["null"]=>
107  NULL
108}
109done!
110