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