PDOサンプル

やりかけだけども、とりあえずこんなものか。

PDO::ATTR_PERSISTENTは持続的設定のオプションなので、この場合はなくてもいいのだけども。
[php num=1] <?php class DBTable {

public static $dbh = NULL;

function construct() { if ( is_null(self::$dbh)) { self::$dbh = new PDO('mysql:host=localhost;dbname=test', 'user', 'password', array(PDO::ATTR_PERSISTENT => true) ); } $this->table = NULL; $this->primarykey = 'id'; } function get($id) { $sql =<<<EOD_GET_QUERY SELECT * FROM {$this->table} WHERE {$this->primarykey} = :id EOD_GET_QUERY;

   $stmt = self::$dbh->prepare($sql);
   $stmt->bindValue(':id', $id);
   $stmt->execute();
   return $stmt->fetch(PDO::FETCH_BOTH);
}

}

class DBTable_test extends DBTable { function construct() { parent::construct(); $this->table = 'test'; $this->primarykey = 'id'; } }

class DBTableFactory { function factory($table) { $classname = 'DBTable_'.$table; return new $classname(); } }

$tbl = DBTableFactory::factory('test'); print_r($tbl->get(2));

exit();[/php]