miniMVC/sys/db.php

143 lines
2.6 KiB
PHP
Raw Normal View History

2011-12-27 13:24:28 -05:00
<?php
/**
* Just for giggles extend PHP's PDO class
*/
class db extends PDO {
private $where;
private static $instance;
2011-12-27 13:24:28 -05:00
2011-12-29 16:57:28 -05:00
public static function get_instance($dbname="default")
2011-12-29 11:24:10 -05:00
{
if ( ! isset(self::$instance))
{
echo 'Creating new instance of db class.';
2011-12-29 16:57:28 -05:00
self::$instance = new db($dbname);
2011-12-29 11:24:10 -05:00
}
return self::$instance;
}
function __construct($dbname="default", $options=array())
2011-12-27 13:24:28 -05:00
{
//Include the database config file
load_file('config/db','app');
2011-12-27 13:24:28 -05:00
// Array manipulation is too verbose
extract($db_conf[$dbname]);
// Sqlite doesn't use dbname param
$dsn = (stripos($type, "sqlite") === FALSE) ? "{$type}:dbname={$db}" : "{$type}:{$db}";
// Set hostname if applicable
if(isset($host))
{
$dsn .= ($host !== "") ? ";host={$host}" : "";
}
// Set port if applicable
if(isset($port))
{
$dsn .= ($port !== "") ? ";port={$port}" : "";
}
if($user === "" && $pass === "")
{
$user = null;
$pass = null;
}
$opts = array(
PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE,
);
if( ! isset($persist))
{
unset($opts[PDO::ATTR_PERSISTENT]);
}
$options = array_merge($opts, $options);
parent::__construct($dsn, $user, $pass, $options);
}
// --------------------------------------------------------------------------
/**
* PHP magic method to facilitate dynamic methods
*
* @param string $name
* @param array $args
*/
function __call($name, $args)
{
if(is_callable($this->$name))
{
//Add $this to the beginning of the args array
array_unshift($args, $this);
//Call the dynamic function
return call_user_func_array($this->$name, $args);
}
}
// --------------------------------------------------------------------------
/**
* Prints out the contents of the object when used as a string
*
* @return string
*/
function __toString()
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$output = '<pre>';
if($method == "var_dump")
{
ob_start();
var_dump($this);
$output .= ob_get_contents();
ob_end_clean();
}
else if($method == "var_export")
{
ob_start();
var_export($this);
$output .= ob_get_contents();
ob_end_clean();
}
else
{
$output .= print_r($this, TRUE);
}
return $output . '</pre>';
}
// --------------------------------------------------------------------------
/**
* Constructor without the "new"
*
* @param array $members
*/
2011-12-29 16:57:28 -05:00
function __invoke($db="default")
2011-12-27 13:24:28 -05:00
{
2011-12-29 16:57:28 -05:00
return self::get_instance($db);
2011-12-27 13:24:28 -05:00
}
2011-12-29 16:57:28 -05:00
2012-01-03 21:54:09 -05:00
function start_transaction()
{
}
2011-12-29 16:57:28 -05:00
2012-01-03 21:54:09 -05:00
function end_transaction()
{
}
2011-12-27 13:24:28 -05:00
}
2011-12-29 16:57:28 -05:00
// End of db.php