简体   繁体   中英

PDO, PHP: Trying to instantiate object to call function - blank page

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

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

PDOExceptin should be PDOException .

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM