xref: /PHP-8.1/ext/pdo/tests/pdo_023.phpt (revision 74859783)
1--TEST--
2PDO Common: extending PDO
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
17class PDOStatementX extends PDOStatement
18{
19    public $test1 = 1;
20
21    protected function __construct()
22    {
23        $this->test2 = 2;
24        $this->test2 = 22;
25        echo __METHOD__ . "()\n";
26    }
27
28    function __destruct()
29    {
30        echo __METHOD__ . "()\n";
31    }
32}
33
34class PDODatabaseX extends PDO
35{
36    public $test1 = 1;
37
38    function __destruct()
39    {
40        echo __METHOD__ . "()\n";
41    }
42
43    function test()
44    {
45        $this->test2 = 2;
46        var_dump($this->test1);
47        var_dump($this->test2);
48        $this->test2 = 22;
49    }
50
51    function query($sql, ...$rest): PDOStatement|false
52    {
53        echo __METHOD__ . "()\n";
54        $stmt = parent::prepare($sql, array(PDO::ATTR_STATEMENT_CLASS=>array('PDOStatementx')));
55        $stmt->execute();
56        return $stmt;
57    }
58}
59
60$db = PDOTest::factory('PDODatabaseX');
61$db->test();
62var_dump($db);
63
64$db->query('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10))');
65$db->query('INSERT INTO test VALUES(0, \'A\')');
66$db->query('INSERT INTO test VALUES(1, \'B\')');
67
68
69$stmt = $db->query('SELECT val, id FROM test');
70var_dump($stmt);
71var_dump($stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE));
72
73$stmt = NULL;
74$db = NULL;
75
76
77?>
78--EXPECTF--
79int(1)
80int(2)
81object(PDODatabaseX)#%d (2) {
82  ["test1"]=>
83  int(1)
84  ["test2"]=>
85  int(22)
86}
87PDODatabaseX::query()
88PDOStatementX::__construct()
89PDOStatementX::__destruct()
90PDODatabaseX::query()
91PDOStatementX::__construct()
92PDOStatementX::__destruct()
93PDODatabaseX::query()
94PDOStatementX::__construct()
95PDOStatementX::__destruct()
96PDODatabaseX::query()
97PDOStatementX::__construct()
98object(PDOStatementX)#%d (3) {
99  ["queryString"]=>
100  string(24) "SELECT val, id FROM test"
101  ["test1"]=>
102  int(1)
103  ["test2"]=>
104  int(22)
105}
106array(2) {
107  ["A"]=>
108  string(1) "0"
109  ["B"]=>
110  string(1) "1"
111}
112PDOStatementX::__destruct()
113PDODatabaseX::__destruct()
114