xref: /PHP-8.3/ext/pdo/tests/pdo_026.phpt (revision 74859783)
1--TEST--
2PDO Common: extending PDO (2)
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        echo __METHOD__ . "()\n";
31    }
32
33    function __destruct()
34    {
35        echo __METHOD__ . "()\n";
36    }
37}
38
39class PDODatabase extends PDO
40{
41    function __destruct()
42    {
43        echo __METHOD__ . "()\n";
44    }
45
46    function query($sql, ...$rest): PDOStatement|false
47    {
48        echo __METHOD__ . "()\n";
49        $stmt = $this->prepare($sql, array(PDO::ATTR_STATEMENT_CLASS=>array('PDOStatementx', array($this))));
50        $stmt->setFetchMode(PDO::FETCH_ASSOC);
51        $stmt->execute();
52        return $stmt;
53    }
54}
55
56$db = PDOTest::factory('PDODatabase');
57var_dump(get_class($db));
58
59$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
60
61$stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)");
62var_dump(get_class($stmt));
63foreach ($data as $row) {
64    $stmt->execute($row);
65}
66
67unset($stmt);
68
69$stmt = $db->query('SELECT * FROM test');
70var_dump(get_class($stmt));
71var_dump(get_class($stmt->dbh));
72
73foreach($stmt as $obj) {
74    var_dump($obj);
75}
76
77echo "===DONE===\n";
78?>
79--EXPECT--
80string(11) "PDODatabase"
81string(12) "PDOStatement"
82PDODatabase::query()
83PDOStatementX::__construct()
84string(13) "PDOStatementX"
85string(11) "PDODatabase"
86array(3) {
87  ["id"]=>
88  string(2) "10"
89  ["val"]=>
90  string(3) "Abc"
91  ["val2"]=>
92  string(3) "zxy"
93}
94array(3) {
95  ["id"]=>
96  string(2) "20"
97  ["val"]=>
98  string(3) "Def"
99  ["val2"]=>
100  string(3) "wvu"
101}
102array(3) {
103  ["id"]=>
104  string(2) "30"
105  ["val"]=>
106  string(3) "Ghi"
107  ["val2"]=>
108  string(3) "tsr"
109}
110===DONE===
111PDOStatementX::__destruct()
112PDODatabase::__destruct()
113