xref: /PHP-8.1/ext/pdo/tests/pdo_011.phpt (revision 74859783)
1--TEST--
2PDO Common: PDO::FETCH_FUNC and statement overloading
3--EXTENSIONS--
4pdo
5--SKIPIF--
6<?php
7$dir = getenv('REDIR_TEST_DIR');
8if (false == $dir) die('skip no driver');
9require_once $dir . 'pdo_test.inc';
10PDOTest::skip();
11?>
12--FILE--
13<?php
14if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
15require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
16$db = PDOTest::factory();
17
18$db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val VARCHAR(10), grp VARCHAR(10))');
19$db->exec('INSERT INTO test VALUES(1, \'A\', \'Group1\')');
20$db->exec('INSERT INTO test VALUES(2, \'B\', \'Group1\')');
21$db->exec('INSERT INTO test VALUES(3, \'C\', \'Group2\')');
22$db->exec('INSERT INTO test VALUES(4, \'D\', \'Group2\')');
23
24class DerivedStatement extends PDOStatement
25{
26    private function __construct($name, $db)
27    {
28        $this->name = $name;
29        echo __METHOD__ . "($name)\n";
30    }
31
32    function reTrieve($id, $val) {
33        echo __METHOD__ . "($id,$val)\n";
34        return array($id=>$val);
35    }
36}
37
38$select1 = $db->prepare('SELECT grp, id FROM test');
39$select2 = $db->prepare('SELECT id, val FROM test');
40$derived = $db->prepare('SELECT id, val FROM test', array(PDO::ATTR_STATEMENT_CLASS=>array('DerivedStatement', array('Overloaded', $db))));
41
42class Test1
43{
44    public function __construct($id, $val)
45    {
46        echo __METHOD__ . "($id,$val)\n";
47        $this->id = $id;
48        $this->val = $val;
49    }
50
51    static public function factory($id, $val)
52    {
53        echo __METHOD__ . "($id,$val)\n";
54        return new self($id, $val);
55    }
56}
57
58function test($id,$val='N/A')
59{
60    echo __METHOD__ . "($id,$val)\n";
61    return array($id=>$val);
62}
63
64$f = new Test1(0,0);
65
66$select1->execute();
67var_dump($select1->fetchAll(PDO::FETCH_FUNC|PDO::FETCH_GROUP, 'test'));
68
69$select2->execute();
70var_dump($select2->fetchAll(PDO::FETCH_FUNC, 'test'));
71
72$select2->execute();
73var_dump($select2->fetchAll(PDO::FETCH_FUNC, array('Test1','factory')));
74
75$select2->execute();
76var_dump($select2->fetchAll(PDO::FETCH_FUNC, array($f, 'factory')));
77
78var_dump(get_class($derived));
79$derived->execute();
80var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'retrieve')));
81$derived->execute();
82var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'reTrieve')));
83$derived->execute();
84var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'RETRIEVE')));
85
86?>
87--EXPECTF--
88DerivedStatement::__construct(Overloaded)
89Test1::__construct(0,0)
90test(1,N/A)
91test(2,N/A)
92test(3,N/A)
93test(4,N/A)
94array(2) {
95  ["Group1"]=>
96  array(2) {
97    [0]=>
98    array(1) {
99      [1]=>
100      string(3) "N/A"
101    }
102    [1]=>
103    array(1) {
104      [2]=>
105      string(3) "N/A"
106    }
107  }
108  ["Group2"]=>
109  array(2) {
110    [0]=>
111    array(1) {
112      [3]=>
113      string(3) "N/A"
114    }
115    [1]=>
116    array(1) {
117      [4]=>
118      string(3) "N/A"
119    }
120  }
121}
122test(1,A)
123test(2,B)
124test(3,C)
125test(4,D)
126array(4) {
127  [0]=>
128  array(1) {
129    [1]=>
130    string(1) "A"
131  }
132  [1]=>
133  array(1) {
134    [2]=>
135    string(1) "B"
136  }
137  [2]=>
138  array(1) {
139    [3]=>
140    string(1) "C"
141  }
142  [3]=>
143  array(1) {
144    [4]=>
145    string(1) "D"
146  }
147}
148Test1::factory(1,A)
149Test1::__construct(1,A)
150Test1::factory(2,B)
151Test1::__construct(2,B)
152Test1::factory(3,C)
153Test1::__construct(3,C)
154Test1::factory(4,D)
155Test1::__construct(4,D)
156array(4) {
157  [0]=>
158  object(Test1)#%d (2) {
159    ["id"]=>
160    string(1) "1"
161    ["val"]=>
162    string(1) "A"
163  }
164  [1]=>
165  object(Test1)#%d (2) {
166    ["id"]=>
167    string(1) "2"
168    ["val"]=>
169    string(1) "B"
170  }
171  [2]=>
172  object(Test1)#%d (2) {
173    ["id"]=>
174    string(1) "3"
175    ["val"]=>
176    string(1) "C"
177  }
178  [3]=>
179  object(Test1)#%d (2) {
180    ["id"]=>
181    string(1) "4"
182    ["val"]=>
183    string(1) "D"
184  }
185}
186Test1::factory(1,A)
187Test1::__construct(1,A)
188Test1::factory(2,B)
189Test1::__construct(2,B)
190Test1::factory(3,C)
191Test1::__construct(3,C)
192Test1::factory(4,D)
193Test1::__construct(4,D)
194array(4) {
195  [0]=>
196  object(Test1)#%d (2) {
197    ["id"]=>
198    string(1) "1"
199    ["val"]=>
200    string(1) "A"
201  }
202  [1]=>
203  object(Test1)#%d (2) {
204    ["id"]=>
205    string(1) "2"
206    ["val"]=>
207    string(1) "B"
208  }
209  [2]=>
210  object(Test1)#%d (2) {
211    ["id"]=>
212    string(1) "3"
213    ["val"]=>
214    string(1) "C"
215  }
216  [3]=>
217  object(Test1)#%d (2) {
218    ["id"]=>
219    string(1) "4"
220    ["val"]=>
221    string(1) "D"
222  }
223}
224string(16) "DerivedStatement"
225DerivedStatement::reTrieve(1,A)
226DerivedStatement::reTrieve(2,B)
227DerivedStatement::reTrieve(3,C)
228DerivedStatement::reTrieve(4,D)
229array(4) {
230  [0]=>
231  array(1) {
232    [1]=>
233    string(1) "A"
234  }
235  [1]=>
236  array(1) {
237    [2]=>
238    string(1) "B"
239  }
240  [2]=>
241  array(1) {
242    [3]=>
243    string(1) "C"
244  }
245  [3]=>
246  array(1) {
247    [4]=>
248    string(1) "D"
249  }
250}
251DerivedStatement::reTrieve(1,A)
252DerivedStatement::reTrieve(2,B)
253DerivedStatement::reTrieve(3,C)
254DerivedStatement::reTrieve(4,D)
255array(4) {
256  [0]=>
257  array(1) {
258    [1]=>
259    string(1) "A"
260  }
261  [1]=>
262  array(1) {
263    [2]=>
264    string(1) "B"
265  }
266  [2]=>
267  array(1) {
268    [3]=>
269    string(1) "C"
270  }
271  [3]=>
272  array(1) {
273    [4]=>
274    string(1) "D"
275  }
276}
277DerivedStatement::reTrieve(1,A)
278DerivedStatement::reTrieve(2,B)
279DerivedStatement::reTrieve(3,C)
280DerivedStatement::reTrieve(4,D)
281array(4) {
282  [0]=>
283  array(1) {
284    [1]=>
285    string(1) "A"
286  }
287  [1]=>
288  array(1) {
289    [2]=>
290    string(1) "B"
291  }
292  [2]=>
293  array(1) {
294    [3]=>
295    string(1) "C"
296  }
297  [3]=>
298  array(1) {
299    [4]=>
300    string(1) "D"
301  }
302}
303