$dsn is de data source name — die draagt je hostnaam en database voor je. Je bouwt hem zo op:
$dsn = 'mysql:dbname=YOUR_DB_NAME;host=YOUR_HOSTNAME' Met $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); heb je PDO verteld uitzonderingen te gooien bij fouten, wat betekent dat je fouten kunt afhandelen in uitzonderingsafhandelaars binnen je subklasse. Een hulpmethode zoals getAssoc zou er dan zo uitzien:
/// Get an associative array of results for the sql. public function getAssoc($sql, $params=array()) { try { $stmt = $this->prepare($sql); $params = is_array($params) ? $params : array($params); $stmt->execute($params); return $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) { // Echo the error or Re-throw it to catch it higher up where you have more // information on where it occurred in your program. // e.g echo 'Error: ' . $e->getMessage(); throw new Exception( __METHOD__ . 'Exception Raised for sql: ' . var_export($sql, true) . ' Params: ' . var_export($params, true) . ' Error_Info: ' . var_export($this->errorInfo(), true), 0, $e); } } Merk op dat de methode een prepared statement met gebonden $params gebruikt, zodat hij zowel herbruikbaar als veilig tegen injectie is — een goed patroon om in je uitgebreide PDO-klasse in te bakken.