xref: /PHP-7.2/ext/pdo/tests/pdo_029.phpt (revision 17ccbeec)
1--TEST--
2PDO Common: extending PDO (3)
3--SKIPIF--
4<?php # vim:ft=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='.dirname(__FILE__) . '/../../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    function execute($params = array())
38    {
39    	echo __METHOD__ . "()\n";
40		parent::execute();
41    }
42}
43
44class PDODatabase extends PDO
45{
46    function __destruct()
47    {
48    	echo __METHOD__ . "()\n";
49    }
50
51    function query($sql)
52    {
53    	echo __METHOD__ . "()\n";
54    	$stmt = $this->prepare($sql, array(PDO::ATTR_STATEMENT_CLASS=>array('PDOStatementx', array($this))));
55    	$stmt->setFetchMode(PDO::FETCH_ASSOC);
56    	$stmt->execute();
57    	return $stmt;
58    }
59}
60
61$db = PDOTest::factory('PDODatabase');
62var_dump(get_class($db));
63
64$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
65
66$stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)");
67var_dump(get_class($stmt));
68foreach ($data as $row) {
69    $stmt->execute($row);
70}
71
72unset($stmt);
73
74echo "===QUERY===\n";
75
76$stmt = $db->query('SELECT * FROM test');
77var_dump(get_class($stmt));
78var_dump(get_class($stmt->dbh));
79
80echo "===FOREACH===\n";
81
82foreach($stmt as $obj) {
83	var_dump($obj);
84}
85
86echo "===DONE===\n";
87exit(0);
88?>
89--EXPECT--
90string(11) "PDODatabase"
91string(12) "PDOStatement"
92===QUERY===
93PDODatabase::query()
94PDOStatementX::__construct()
95PDOStatementX::execute()
96string(13) "PDOStatementX"
97string(11) "PDODatabase"
98===FOREACH===
99array(3) {
100  ["id"]=>
101  string(2) "10"
102  ["val"]=>
103  string(3) "Abc"
104  ["val2"]=>
105  string(3) "zxy"
106}
107array(3) {
108  ["id"]=>
109  string(2) "20"
110  ["val"]=>
111  string(3) "Def"
112  ["val2"]=>
113  string(3) "wvu"
114}
115array(3) {
116  ["id"]=>
117  string(2) "30"
118  ["val"]=>
119  string(3) "Ghi"
120  ["val2"]=>
121  string(3) "tsr"
122}
123===DONE===
124PDOStatementX::__destruct()
125PDODatabase::__destruct()
126