调用成员函数prepare()怎么了?面向对象 [英] Call to a member function prepare() What's wrong with this? OOP

查看:33
本文介绍了调用成员函数prepare()怎么了?面向对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有这个类(只会包含标头和第一个函数).

Okay I have this class (Will only include header, and first function).

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

由于某种原因,我现在收到此错误

For some reason I am now getting this error

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

之前运行良好,在转换为使用自动加载类后开始运行.

It worked fine before, it starting doign this after I converted to use autoload classes.

注册页面:

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

$users = new Users;

这就是我使用该函数进行注册的方式(这里发生错误):

And that's how I use the function to register (error happens here):

    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表示不是?...

I am including db.inc.php, which has the DB connection in it with var $pdo which is an object, PHP says it's not?...

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

我做错了什么?为什么这样做呢?非常感谢.

What did I do wrong? Why does it do that? Thanks A lot.

这就是我自动加载的方式:

and that's how I auto load:

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

spl_autoload_register('classAutoload');

推荐答案

将$ pdo放入需要它的对象的最佳方法是依赖注入.这样.

The best way to get $pdo into an object that needs it is with dependency injection. Like this.

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

这篇关于调用成员函数prepare()怎么了?面向对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆