Code: Select all
function __autoload($name) {
$root = getRoot();
$theClass = $root . 'classes/' . $name . '.php';
if (file_exists($theClass)) {
include($theClass);
} else {
die('Tried to load non-existent class: ' . $theClass);
}
}
Code: Select all
function getRoot($uri = '') {
if ($uri == '') {
$uri = $_SERVER['PHP_SELF'];
}
$depth = substr_count($uri, '/')-1;
$root = '';
for ($x = 1; $x <= $depth; $x++)
$root = '../' . $root;
return $root;
}
Code: Select all
<?php
/*
* Database class only one connection is allowed.
*/
class Database extends PDO{
private $connection;
private static $instance;
public static function getInstance() {
if (!self::$instance) {
self::$instance = new self("mysql:host=". DBHOST . ";dbname=". DB ,DBUSER,DBPASS);
}
return self::$instance;
}
/*
* empty clone magic method to prevent duplication
*
*/
private function __clone() {
}
}
Code: Select all
$db = Database::getInstance();
I've demonstrated use of these concepts in several posts on this forum, so I'll leave it as an exercise for you to find one of them if you need to see an example of the code working before you implement it.