1--TEST-- 2PDO Common: extending PDO (4) 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 $this->setFetchMode(PDO::FETCH_ASSOC); 30 echo __METHOD__ . "()\n"; 31 } 32 33 function __destruct() 34 { 35 echo __METHOD__ . "()\n"; 36 } 37 38 function execute($params = array()) 39 { 40 echo __METHOD__ . "()\n"; 41 parent::execute(); 42 } 43} 44 45class PDODatabase extends PDO 46{ 47 function __destruct() 48 { 49 echo __METHOD__ . "()\n"; 50 } 51 52 function query($sql) 53 { 54 echo __METHOD__ . "()\n"; 55 return parent::query($sql); 56 } 57} 58 59$db = PDOTest::factory('PDODatabase'); 60var_dump(get_class($db)); 61 62$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))'); 63 64$stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)"); 65var_dump(get_class($stmt)); 66foreach ($data as $row) { 67 $stmt->execute($row); 68} 69 70unset($stmt); 71 72echo "===QUERY===\n"; 73 74var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS)); 75$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatementx', array($db))); 76var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS)); 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--EXPECTF-- 91string(11) "PDODatabase" 92string(12) "PDOStatement" 93===QUERY=== 94array(1) { 95 [0]=> 96 string(12) "PDOStatement" 97} 98array(2) { 99 [0]=> 100 string(13) "PDOStatementX" 101 [1]=> 102 array(1) { 103 [0]=> 104 object(PDODatabase)#%d (0) { 105 } 106 } 107} 108PDODatabase::query() 109PDOStatementX::__construct() 110string(13) "PDOStatementX" 111string(11) "PDODatabase" 112===FOREACH=== 113array(3) { 114 ["id"]=> 115 string(2) "10" 116 ["val"]=> 117 string(3) "Abc" 118 ["val2"]=> 119 string(3) "zxy" 120} 121array(3) { 122 ["id"]=> 123 string(2) "20" 124 ["val"]=> 125 string(3) "Def" 126 ["val2"]=> 127 string(3) "wvu" 128} 129array(3) { 130 ["id"]=> 131 string(2) "30" 132 ["val"]=> 133 string(3) "Ghi" 134 ["val2"]=> 135 string(3) "tsr" 136} 137===DONE=== 138PDOStatementX::__destruct() 139PDODatabase::__destruct() 140