0

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...

1
  • var_dump($e); before exiting... Commented Apr 27, 2013 at 18:32

3 Answers 3

2

You have a few errors here (use ini_set("display_errors", 1); error_reporting(-1); to see all your error messages):

The exception class of PDO is named PDOException and not PDOExceptin.

You call a static function from an non-static context: $database->getDb() where getDb is a static method. (write Database::getDb())

You write new Database which will result in a fatal error as the constructor is private (and named constructors have lower precedence than the magic method). Use there:

$getdb = Database::Database(); // and declare your Database method as static
Sign up to request clarification or add additional context in comments.

Comments

1

PDOExceptin should be PDOException.

Also, it helps to turn display_errors on and to install xdebug when developing.

Comments

1

Obviously, I'm doing something wrong here.

Yes. you're writing way too much code.
The more code you write, the more errors you have. So, just get rid of all the useless code:

class Database {           
    private static $datasource='mysql:host=localhost; dbname=db_name';
    private static $username='root';
    private static $password='root';
    private static $db;      

    public static function getDB(){ 
      if(!isset(self::$db)){ 
          self::$db=new PDO(self::$datasource,self::$username,self::$password); 
      }
      return self::$db;      
    }
}

and call it this way

$db = Database::getDB();

4 Comments

And then, one day he'll need two DB instances and... huhoh. :D
@Denis two DB instances? why? doesn't make any sense to have multiple instances... (If you don't need to manipulate data of different servers, which is a very very rare case)
@bwoebi: It depends on the app you use, on what you're doing, on whether you decided to scale by sharding your db, etc.
@Denis yes, but then you'll have some real abstraction layer who checks what to do ad you contain you connections in some static array etc....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.