1--TEST-- 2PDO Common: extending PDO (3) 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$data = array( 18 array('10', 'Abc', 'zxy'), 19 array('20', 'Def', 'wvu'), 20 array('30', 'Ghi', 'tsr'), 21); 22 23class PDOStatementX extends PDOStatement 24{ 25 public $dbh; 26 27 protected function __construct($dbh) 28 { 29 $this->dbh = $dbh; 30 echo __METHOD__ . "()\n"; 31 } 32 33 function __destruct() 34 { 35 echo __METHOD__ . "()\n"; 36 } 37 38 function execute($params = array()): bool 39 { 40 echo __METHOD__ . "()\n"; 41 return parent::execute(); 42 } 43} 44 45class PDODatabase extends PDO 46{ 47 function __destruct() 48 { 49 echo __METHOD__ . "()\n"; 50 } 51 52 function query($sql, ...$rest): PDOStatement|false 53 { 54 echo __METHOD__ . "()\n"; 55 $stmt = $this->prepare($sql, array(PDO::ATTR_STATEMENT_CLASS=>array('PDOStatementx', array($this)))); 56 $stmt->setFetchMode(PDO::FETCH_ASSOC); 57 $stmt->execute(); 58 return $stmt; 59 } 60} 61 62$db = PDOTest::factory('PDODatabase'); 63var_dump(get_class($db)); 64 65$db->exec('CREATE TABLE test029(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))'); 66 67$stmt = $db->prepare("INSERT INTO test029 VALUES(?, ?, ?)"); 68var_dump(get_class($stmt)); 69foreach ($data as $row) { 70 $stmt->execute($row); 71} 72 73unset($stmt); 74 75echo "===QUERY===\n"; 76 77$stmt = $db->query('SELECT * FROM test029'); 78var_dump(get_class($stmt)); 79var_dump(get_class($stmt->dbh)); 80 81echo "===FOREACH===\n"; 82 83foreach($stmt as $obj) { 84 var_dump($obj); 85} 86 87echo "===DONE===\n"; 88exit(0); 89?> 90--CLEAN-- 91<?php 92require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; 93$db = PDOTest::factory(); 94PDOTest::dropTableIfExists($db, "test029"); 95?> 96--EXPECT-- 97string(11) "PDODatabase" 98string(12) "PDOStatement" 99===QUERY=== 100PDODatabase::query() 101PDOStatementX::__construct() 102PDOStatementX::execute() 103string(13) "PDOStatementX" 104string(11) "PDODatabase" 105===FOREACH=== 106array(3) { 107 ["id"]=> 108 string(2) "10" 109 ["val"]=> 110 string(3) "Abc" 111 ["val2"]=> 112 string(3) "zxy" 113} 114array(3) { 115 ["id"]=> 116 string(2) "20" 117 ["val"]=> 118 string(3) "Def" 119 ["val2"]=> 120 string(3) "wvu" 121} 122array(3) { 123 ["id"]=> 124 string(2) "30" 125 ["val"]=> 126 string(3) "Ghi" 127 ["val2"]=> 128 string(3) "tsr" 129} 130===DONE=== 131PDOStatementX::__destruct() 132PDODatabase::__destruct() 133