xref: /php-src/ext/pdo/tests/pdo_026.phpt (revision f4a5db3e)
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 test026(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
60
61$stmt = $db->prepare("INSERT INTO test026 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 test026');
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--CLEAN--
80<?php
81require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
82$db = PDOTest::factory();
83PDOTest::dropTableIfExists($db, "test026");
84?>
85--EXPECT--
86string(11) "PDODatabase"
87string(12) "PDOStatement"
88PDODatabase::query()
89PDOStatementX::__construct()
90string(13) "PDOStatementX"
91string(11) "PDODatabase"
92array(3) {
93  ["id"]=>
94  string(2) "10"
95  ["val"]=>
96  string(3) "Abc"
97  ["val2"]=>
98  string(3) "zxy"
99}
100array(3) {
101  ["id"]=>
102  string(2) "20"
103  ["val"]=>
104  string(3) "Def"
105  ["val2"]=>
106  string(3) "wvu"
107}
108array(3) {
109  ["id"]=>
110  string(2) "30"
111  ["val"]=>
112  string(3) "Ghi"
113  ["val2"]=>
114  string(3) "tsr"
115}
116===DONE===
117PDOStatementX::__destruct()
118PDODatabase::__destruct()
119