f1 = $f1; $this->f2 = $f2; } } interface T1DatabaseAccess { public function get_one($f2); public function get_all(); public function put($o); public function remove($f1); } class T1DatabaseAccessPDO implements T1DatabaseAccess { private $constr; private $username; private $password; public function __construct($constr, $username, $password) { $this->constr = $constr; $this->username = $username; $this->password = $password; } private function get_connection() { // connect to server and select database $con = new PDO($this->constr, $this->username, $this->password); // use exceptions instead of error results $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // get columns by name $con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // return connection (note: no globals) return $con; } public function get_one($f2) { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement $stmt = $con->prepare('SELECT F1,F2 FROM T1 WHERE F2=:F2'); // execute with parameters $stmt->execute(array(':F2' => $f2)); // get first row and field F1 if($row = $stmt->fetch()) { $res = new T1($row['F1'], $row['F2']); } else { die("$f2 not found"); } // return value return $res; } public function get_all() { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement $stmt = $con->prepare('SELECT F1,F2 FROM T1'); // execute with no parameters $stmt->execute(array()); // create array to contain rows $res = array(); // get all rows while($row = $stmt->fetch()) { $res[] = new T1($row['F1'], $row['F2']); } // return array with rows return $res; } public function put($o) { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement $stmt = $con->prepare('INSERT INTO T1(F1,F2) VALUES(:F1,:F2)'); // execute with parameters $stmt->execute(array(':F1' => $o->f1, ':F2' => $o->f2)); } public function remove($f1) { // connect to server and select database $con = $this->get_connection(); // prepare SQL statement $stmt = $con->prepare('DELETE FROM T1 WHERE F1=:F1'); // execute with parameters $stmt->execute(array(':F1' => $f1)); } } class T1DatabaseAccessFactory { const PDO = 'PDO'; public static function get_database_access($id, $constr, $username, $password) { if($id === T1DatabaseAccessFactory::PDO) { return new T1DatabaseAccessPDO($constr, $username, $password); } else { throw new Exception("Unknown T1DatabaseAccess id: $id"); } } } interface T1Presenter { public function display($data); } class T1PresenterHtmlTable implements T1Presenter { public function display($data) { $rows = ""; foreach($data as $row) { $f1 = $row->f1; $f2 = $row->f2; $rows .= sprintf("\r\n$f1\r\n$f2\r\n\r\n"); } return "\r\n\r\n\r\n\r\n\r\n$rows
F1F2
\r\n"; } } class T1PresenterFactory { const HTML_TABLE = 'HTML Table'; public static function get_presenter($id) { if($id === T1PresenterFactory::HTML_TABLE) { return new T1PresenterHtmlTable() ; } else { throw new Exception("Unknown T1Presenter id: $id"); } } } function test() { $data_access = T1DatabaseAccessFactory::get_database_access(T1DatabaseAccessFactory::PDO, 'mysql:host=localhost;dbname=Test', 'root', ''); $presenter = T1PresenterFactory::get_presenter(T1PresenterFactory::HTML_TABLE); $f1 = $data_access->get_one('BB')->f1; echo "$f1
\r\n"; $data = $data_access->get_all(); echo $presenter->display($data); $data_access->put(new T1(999, 'XXX')); $data = $data_access->get_all(); echo $presenter->display($data); $data_access->remove(999); $data = $data_access->get_all(); echo $presenter->display($data); } test(); ?>