So I have two files involved in this problem. One of them is the Database class and the other one is the file that include_once the Database file and then goes on to instantiate an object of that class to call a function -- getDB();.
Thats's where it goes wrong.
Database class:
<?php
class Database {
private static $datasource='mysql:host=localhost; dbname=db_name';
private static $username='root';
private static $password='root';
private static $db;
private function __construct(){}
public static function getDB(){
if(!isset(self::$db)){
try{
self::$db=new PDO(self::$datasource,self::$username,self::$password);
}
catch(PDOExceptin $e) {
$error=$e->getMessage(); //variable $error can be used in the database_error.php file
//display database error file.
//include('database_error.php');
exit();
}
}
return self::$db;
}
function Database(){
return new Database;
}
}
?>
And in my main file, I'm doing this:
<?php
include('partials/header.php');
include_once('functions/pdo.php');
$database = new Database();
$getdb = $database->getDB();
//Anything below won't show because of the failed instantiation of Database object above.
//code..
?>
Obviously, I'm doing something wrong here. I'm running MAMP with php 5.3. How can I use my Database correctly? The reason I have a function with the same name as the class is because I read that you could instantiate the object with the function instead, but I didn't get that to work either...
var_dump($e);before exiting...