xref: /PHP-8.2/ext/pdo/tests/pdo_005.phpt (revision 902d6439)
1--TEST--
2PDO Common: PDO::FETCH_CLASS
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), val2 VARCHAR(10))');
19$db->exec("INSERT INTO test VALUES(1, 'A', 'AA')");
20$db->exec("INSERT INTO test VALUES(2, 'B', 'BB')");
21$db->exec("INSERT INTO test VALUES(3, 'C', 'CC')");
22
23$stmt = $db->prepare('SELECT id, val, val2 from test');
24
25class TestBase
26{
27    public $id;
28    protected $val;
29    private $val2;
30}
31
32#[AllowDynamicProperties] // $val2 will be dynamic now.
33class TestDerived extends TestBase
34{
35    protected $row;
36
37    public function __construct(&$row)
38    {
39        echo __METHOD__ . "($row,{$this->id})\n";
40        $this->row = $row++;
41    }
42}
43
44$stmt->execute();
45var_dump($stmt->fetchAll(PDO::FETCH_CLASS));
46
47$stmt->execute();
48var_dump($stmt->fetchAll(PDO::FETCH_CLASS, 'TestBase'));
49
50$stmt->execute();
51var_dump($stmt->fetchAll(PDO::FETCH_CLASS, 'TestDerived', array(0)));
52
53?>
54--EXPECTF--
55array(3) {
56  [0]=>
57  object(stdClass)#%d (3) {
58    ["id"]=>
59    string(1) "1"
60    ["val"]=>
61    string(1) "A"
62    ["val2"]=>
63    string(2) "AA"
64  }
65  [1]=>
66  object(stdClass)#%d (3) {
67    ["id"]=>
68    string(1) "2"
69    ["val"]=>
70    string(1) "B"
71    ["val2"]=>
72    string(2) "BB"
73  }
74  [2]=>
75  object(stdClass)#%d (3) {
76    ["id"]=>
77    string(1) "3"
78    ["val"]=>
79    string(1) "C"
80    ["val2"]=>
81    string(2) "CC"
82  }
83}
84array(3) {
85  [0]=>
86  object(TestBase)#%d (3) {
87    ["id"]=>
88    string(1) "1"
89    ["val":protected]=>
90    string(1) "A"
91    ["val2":"TestBase":private]=>
92    string(2) "AA"
93  }
94  [1]=>
95  object(TestBase)#%d (3) {
96    ["id"]=>
97    string(1) "2"
98    ["val":protected]=>
99    string(1) "B"
100    ["val2":"TestBase":private]=>
101    string(2) "BB"
102  }
103  [2]=>
104  object(TestBase)#%d (3) {
105    ["id"]=>
106    string(1) "3"
107    ["val":protected]=>
108    string(1) "C"
109    ["val2":"TestBase":private]=>
110    string(2) "CC"
111  }
112}
113TestDerived::__construct(0,1)
114TestDerived::__construct(1,2)
115TestDerived::__construct(2,3)
116array(3) {
117  [0]=>
118  object(TestDerived)#%d (5) {
119    ["id"]=>
120    string(1) "1"
121    ["val":protected]=>
122    string(1) "A"
123    ["val2":"TestBase":private]=>
124    NULL
125    ["row":protected]=>
126    int(0)
127    ["val2"]=>
128    string(2) "AA"
129  }
130  [1]=>
131  object(TestDerived)#%d (5) {
132    ["id"]=>
133    string(1) "2"
134    ["val":protected]=>
135    string(1) "B"
136    ["val2":"TestBase":private]=>
137    NULL
138    ["row":protected]=>
139    int(1)
140    ["val2"]=>
141    string(2) "BB"
142  }
143  [2]=>
144  object(TestDerived)#%d (5) {
145    ["id"]=>
146    string(1) "3"
147    ["val":protected]=>
148    string(1) "C"
149    ["val2":"TestBase":private]=>
150    NULL
151    ["row":protected]=>
152    int(2)
153    ["val2"]=>
154    string(2) "CC"
155  }
156}
157