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