Created
November 8, 2012 22:27
-
-
Save MadaraUchiha/4042191 to your computer and use it in GitHub Desktop.
Example of good function construction practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $dsn DSN string to pass the PDO object. Passed in to prevent tight coupling | |
* @param string $user Username for a privileged user in the database. | |
* @param string $password Password matching $user. | |
* | |
* @return \PDO | |
*/ | |
function pdoConnect($dsn, $user, $password) { | |
$pdo = new PDO($dsn, $user, $password); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
return $pdo; | |
} | |
/* Snip */ | |
try { | |
$pdo = pdoConnect("mysql:host=localhost;dbname=database-name", "some_user", "some_password"); | |
$pdo->prepare("Some Query"); | |
//.... | |
} | |
catch (PDOException $e) { | |
//Error handling here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment