xref: /PHP-8.1/ext/pdo/tests/pdo_030.phpt (revision 74859783)
1--TEST--
2PDO Common: extending PDO (4)
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
17$data = array(
18    array('10', 'Abc', 'zxy'),
19    array('20', 'Def', 'wvu'),
20    array('30', 'Ghi', 'tsr'),
21);
22
23class PDOStatementX extends PDOStatement
24{
25    public $dbh;
26
27    protected function __construct($dbh)
28    {
29        $this->dbh = $dbh;
30        $this->setFetchMode(PDO::FETCH_ASSOC);
31        echo __METHOD__ . "()\n";
32    }
33
34    function __destruct()
35    {
36        echo __METHOD__ . "()\n";
37    }
38
39    function execute($params = array()): bool
40    {
41        echo __METHOD__ . "()\n";
42        parent::execute();
43    }
44}
45
46class PDODatabase extends PDO
47{
48    function __destruct()
49    {
50        echo __METHOD__ . "()\n";
51    }
52
53    function query($sql, ...$rest): PDOStatement|false
54    {
55        echo __METHOD__ . "()\n";
56        return parent::query($sql);
57    }
58}
59
60$db = PDOTest::factory('PDODatabase');
61var_dump(get_class($db));
62
63$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
64
65$stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)");
66var_dump(get_class($stmt));
67foreach ($data as $row) {
68    $stmt->execute($row);
69}
70
71unset($stmt);
72
73echo "===QUERY===\n";
74
75var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS));
76$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatementx', array($db)));
77var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS));
78$stmt = $db->query('SELECT * FROM test');
79var_dump(get_class($stmt));
80var_dump(get_class($stmt->dbh));
81
82echo "===FOREACH===\n";
83
84foreach($stmt as $obj) {
85    var_dump($obj);
86}
87
88echo "===DONE===\n";
89exit(0);
90?>
91--EXPECTF--
92string(11) "PDODatabase"
93string(12) "PDOStatement"
94===QUERY===
95array(1) {
96  [0]=>
97  string(12) "PDOStatement"
98}
99array(2) {
100  [0]=>
101  string(13) "PDOStatementX"
102  [1]=>
103  array(1) {
104    [0]=>
105    object(PDODatabase)#%d (0) {
106    }
107  }
108}
109PDODatabase::query()
110PDOStatementX::__construct()
111string(13) "PDOStatementX"
112string(11) "PDODatabase"
113===FOREACH===
114array(3) {
115  ["id"]=>
116  string(2) "10"
117  ["val"]=>
118  string(3) "Abc"
119  ["val2"]=>
120  string(3) "zxy"
121}
122array(3) {
123  ["id"]=>
124  string(2) "20"
125  ["val"]=>
126  string(3) "Def"
127  ["val2"]=>
128  string(3) "wvu"
129}
130array(3) {
131  ["id"]=>
132  string(2) "30"
133  ["val"]=>
134  string(3) "Ghi"
135  ["val2"]=>
136  string(3) "tsr"
137}
138===DONE===
139PDOStatementX::__destruct()
140PDODatabase::__destruct()
141