Skip to content

Instantly share code, notes, and snippets.

@bign8
Last active August 29, 2015 13:56
Show Gist options
  • Save bign8/a6642723e1e261f8ff5e to your computer and use it in GitHub Desktop.
Save bign8/a6642723e1e261f8ff5e to your computer and use it in GitHub Desktop.
An abstraction of PHP's PDO.
  • Pass multiple parameters to StatementHandle->execute(...) instead of an array of arguments.
    • Array of arguments still works for backwards compatibility.
  • Instead of new PDO(...)'s all over your applicaiton, just a simple new myPDO() will work all the time.
<?php
class config {
// For PHPMailer Class for sending email from forms and error reports
const defaultEmail = '';
const defaultFrom = '';
const notifyEmail = '';
const notifyName = '';
// For database connection scheme
const db_dsn = 'mysql:host=%s;dbname=%s'; // %s is replaced in following order (server, name, user, pass)
const db_server = '';
const db_name = '';
const db_user = ''; // also passed to new PDO(...) to allow mysql connections
const db_pass = ''; // also passed to new PDO(...) to allow mysql connections
static $db_opt = array(); // PDO connection options
// Misc use
const encryptSTR = 'RandomSaltString'; // Try using random.org/strings
}
<?php
/**
* Class: myPDO
* Purpose: To simplify the PDO connection process and accuratly report database connection issues
* Notes:
* 1. Constructor takes a DbOptions class to accurately build a Database Source Name (DSN) and connect to a database
* 2. Sets the Statement_Class attribute to use our myPDOStatement rather than the built in PDOStatement
* 3. On error, a report is sent via dbError to the webadmin's email as defined in CoreClass.php (a redirect to /dbError.cc is also fired)
*/
class myPDO extends PDO
{
public function __construct()
{
try {
$dns = sprintf(config::db_dsn, config::db_server, config::db_name, config::db_user, config::db_pass);
parent::__construct( $dns, config::db_user, config::db_pass, config::$db_opt );
$this->setAttribute( PDO::ATTR_STATEMENT_CLASS, array('myPDOStatement') ); // Set Statement_Class
} catch (PDOException $e) {
if( $_SERVER['REQUEST_URI'] != '/db404' ) { // to be implemented
die($e->getMessage());
// This would be a good spot to dispatch a real error and redirect to /db404
}
}
}
}
/**
* Class: myPDOStatement
* Purpose: To extend the features of the default PDOStatement class
* Notes:
* 1. execute now allows us to pass multiple parameters and it will automatically wrap them as an array.
* a. any single variable that is not an array will be wrapped as an array
* b. default functionality is acceptable as well
* c. Usage: $STH->execute($single_param) OR $STH->execute($first, $second, ...) OR $STH->execute(array($first, $second, ...))
*/
class myPDOStatement extends PDOStatement
{
public function execute ( $bound_input_params = NULL )
{
$arr = func_get_args();
$input_parameters = is_array($arr[0]) ? $arr[0] : $arr ;
return parent::execute($input_parameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment