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