调用成员函数prepare()这有什么问题? OOP

时间:2013-05-02 16:42:27

标签: php pdo

好的我有这个类(只包含标题和第一个函数)。

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

之前它工作正常,在我转换为使用自动加载类之后开始执行此操作。

注册页面:

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,其中有一个数据库连接,var $ 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');

1 个答案:

答案 0 :(得分: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);