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 test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))'); 66 67$stmt = $db->prepare("INSERT INTO test 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 test'); 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--EXPECT-- 91string(11) "PDODatabase" 92string(12) "PDOStatement" 93===QUERY=== 94PDODatabase::query() 95PDOStatementX::__construct() 96PDOStatementX::execute() 97string(13) "PDOStatementX" 98string(11) "PDODatabase" 99===FOREACH=== 100array(3) { 101 ["id"]=> 102 string(2) "10" 103 ["val"]=> 104 string(3) "Abc" 105 ["val2"]=> 106 string(3) "zxy" 107} 108array(3) { 109 ["id"]=> 110 string(2) "20" 111 ["val"]=> 112 string(3) "Def" 113 ["val2"]=> 114 string(3) "wvu" 115} 116array(3) { 117 ["id"]=> 118 string(2) "30" 119 ["val"]=> 120 string(3) "Ghi" 121 ["val2"]=> 122 string(3) "tsr" 123} 124===DONE=== 125PDOStatementX::__destruct() 126PDODatabase::__destruct() 127