2013-05-02 5 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 

은 어떤 이유로 나는 지금 내가 자동로드 클래스를 사용하도록 변환이 후 doign 시작, 그것은 잘 이전 작업이 오류

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

을 얻고있다.

등록 페이지 :

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

정말 아무것도 생각할 수 없다. 목적은 var에 $의 PDO와 거기에 DB 연결, PHP는 아니에요?라고있다 내가 db.inc.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

+0

'global $ pdo'는 좋은 생각처럼 들리지 않습니다 ... 그런데'var_dump ($ pdo)'를 사용해 보았습니까? 또한 새로운 PDO()가 try-catch 외부에있는 이유는 무엇입니까? – Uby

+0

PDO 확장 기능이 설치되어 있습니까? http://www.php.net/manual/en/pdo.installation.php – walid

답변

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); 
관련 문제