所以我有兩個文件涉及這個問題。其中一個是Database類,另一個是include_once
Database文件,然後繼續實例化該類的對象以調用函數 - getDB();. 這就是它出錯的地方。PDO,PHP:試圖實例化對象以調用函數 - 空白頁
數據庫類:
<?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;
}
}
?>
而在我的主文件,我這樣做:
<?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..
?>
很顯然,我在這裏做得不對。我使用php 5.3運行MAMP。 如何正確使用我的數據庫?我之所以具有相同名稱的函數作爲類是因爲我讀了那麼你可以用函數的對象,而不是,但我沒有得到這兩種工作...
'的var_dump($ E);'退出前... – 2013-04-27 18:32:36