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