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