2013-05-02 32 views
0

好吧,我有這個類(將只包含標題和第一個函數)。調用成員函數prepare()這有什麼問題? OOP

require_once("./inc/db.inc.php"); 

class Users 
{ 



    /** 
    * Properties 
    **/ 

    private $insert; 

    protected $user; 

    protected $email; 

    protected $get; 

    protected $check; 

    protected $generate; 

    protected $drop; 


    /** 
    * PUBLIC function Register 
    * 
    * Registers the user to the system, checking for errors. 
    * If error was found, it will throw new exception. 
    * 
    * @parm username The username the user posted. 
    * @parm password The password the user posted. 
    * @parm repassword The validated password the user posted. 
    * @parm email The email the user posted. 
    * @parm reemail The validated email the user posted. 
    * @parm day The day the user posted (for date of birth). 
    * @parm month The month the user posted (for date of birth). 
    * @parm year The year the user posted (for date of birth). 
    * 
    * @return Return true means everything is correct, register successfully. 
    **/ 

    public function register($username, $password, $repassword, $email, $reemail, $day, $month, $year) 
    { 

     global $pdo; 

     // Check if passwords matching. 
     if ($password != $repassword) 
     { 
      throw new exception ("Passwords does not match."); 
     } 
     // Check if emails matching. 
     else if ($email != $reemail) 
     { 
      throw new exception ("Emails does not match."); 
     } 

     // The main insert query 
     $this->insert = $pdo->prepare 
     (" 
      INSERT INTO users 
      (user_name, user_password, user_email, user_birth) 
      VALUES 
      (:username, :password, :email, :birth) 
     "); 
... and so on...^error is there 

出於某種原因,我現在正在此錯誤

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\drip\class\users.class.php on line 68 

它之前工作得很好,它開始doign這之後我轉換爲使用自動加載類。

註冊頁面:

include ("inc/config.inc.php"); 

$users = new Users; 

這就是我如何使用函數來註冊(錯誤發生在這裏):

try 
    { 
     $users->register($_POST['user'], $_POST['pass'], $_POST['repass'], $_POST['email'], $_POST['reemail'], $_POST['day'], $_POST['month'], $_POST['year']); 
     echo 'Successfully Registered!'; 
    } 
    catch (Exception $e) 
    { 
     echo $e->getMessage(); 
    } 

我實在想不出任何東西。

我包括db.inc.php,這在其與變量$ PDO數據庫連接是一個對象,PHP說,這是不是這樣呢?

 $pdo = new PDO('mysql:host='.MYSQL_HOST.';dbname=driptone', MYSQL_USER, MYSQL_PASSWORD);   

     try 
     { 
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     } 

     /** 
     * Connection failed, we will print an error. 
     * @var e holds the error message. 
     **/ 

     catch(PDOException $e) 
     { 
      echo $e->getMessage(); 
     } 

我做了什麼錯?它爲什麼這樣做?非常感謝。

,這就是我的汽車負載如何:

function classAutoLoad($class) { 
    if (file_exists("class/$class.class.php")) 
     include("class/".$class.".class.php"); 
} 

spl_autoload_register('classAutoload'); 
+0

我把在somde debuggering - '的var_dump($ PDO)'以確保它真正被設置正確。當你在db.inc.php中初始化它時,它不在函數內部,或者它是什麼? – andrewsi 2013-05-02 16:48:55

+0

'global $ pdo'聽起來不是個好主意......順便說一句,你是否嘗試過'var_dump($ pdo)'來檢查裏面的內容?另外,爲什麼在try-catch之外的新的PDO()? – Uby 2013-05-02 16:49:20

+0

您確定安裝了PDO擴展? http://www.php.net/manual/en/pdo.installation.php – walid 2013-05-02 16:57:56

回答

0

最好的方式來獲得$ PDO到需要它是依賴注入的對象。喜歡這個。

class Users { 
    private $insert; 
    ... 
    private $pdo; 

    public function __construct($pdo) { 
     $this->pdo = $pdo; 
    } 

    public function register(...) { 
     ... 
     $this->pdo->prepare(...); 
     ... 
    } 
} 

然後在你的註冊頁面

include ("inc/config.inc.php"); 
include ("inc/db.inc.php"); 
$users = new Users($pdo);