php oop类彼此不能很好玩

时间:2012-01-19 04:01:38

标签: php oop

问题:我有2个类,DB类和一个User类,如果放在同一个类中将会起作用,但是当我分离它们时,我无法弄清楚如何使用DB类来连接用户类。

我有'DBinterface'类和'用户'类。

*** dbinterface.php

<? class dbinterface {
    var $dbHost,
        $dbUser,
        $dbName,
        $dbPass,
        $dbUserTable;


    function User() {
        $this->dbHost = 'host':
        $this->dbUser = 'user';
        $this->dbName = 'name';
        $this->dbPass = 'pass';
        $this->dbUserTable = 'table';
    }

} // End dbinterface class definition ?> 

*** user.php的

<?
include('dbinterface.php');
    class User {

        var $userID,
            $userName,
            $userPassword;

        function registerUser($userName, $userPassword) {
            // Connect to database
            $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
            if(!$dbLink) die("Could not connect to database. " . mysql_error());

            // Select database
            mysql_select_db($this->dbName);

            // Insert data
            $query = "insert into $this->dbUserTable values (NULL, \"$userName\", \"$userPassword\")";
            $result = mysql_query($query);

            // Test to make sure query worked
            if(!$result) die("Query didn't work. " . mysql_error());

            // Get the user ID
            $this->userID = mysql_insert_id();

            // Close database connection
            mysql_close($dbLink);

            // Assign the values to the data members
            $this->userName = $userName;
            $this->userPassword = $userPassword;
        } // End registerUser() ?>

PS:我已经删除了安全性和其他mumbojumbo以便于阅读。  任何和所有帮助都非常适合!

2 个答案:

答案 0 :(得分:3)

好。我会试着想象你想做什么:) 您需要一个用于存储db变量的类,例如用户名,密码等,以及用于db-class params的第二类。

/ * file1 * /

Class db {

    private $_dbLink;
    private $_dbHost = 'host';
    private $_dbUser = 'user';
    private $_dbName = 'name';
    private $_dbPass = 'pass';
    private $_dbUserTable = 'table';

    public function connect ()
    {
        $this->_dbLink = mysql_connect($this->_dbHost, $this->_dbUser, $this->_dbPass);
        if(!$this->_dbLink) 
            throw new Exception ("Could not connect to database. " . mysql_error());
    }

    public function getLink()
    {
        return $this->_dbLink;
    }

    public function getUserName ()
    {
        return $this->dbUser;
    }

    public function getUserPass ()
    {
        return $this->_dbPass;
    }

    /* create the same methods for each variable */
}

/ * file2 * /

Class User {

    public function registerUser (/* params */)
    {
        $db = new db();
        $db->connect();

        /* your code */
        // Select database
        mysql_select_db($db->getDb(), $db->getLink());

        // Insert data
        $query = "insert into ".$db->getTable()." values (NULL, \"$userName\", \"$userPassword\")"; /* here is SQL-injection */
        $result = mysql_query($query, $db->getLink());
        /* and so on ...... */
    }
}

/ * file3 * /

// include file1
// include file2
$user = new User;
$user->registerUser(/* params */);

这是快速解决方案。您应该学习如何创建类和项目架构。使用php.net获取更多信息。阅读有关面向对象的编程。并阅读有关更安全代码的SQL注入。

答案 1 :(得分:0)

$ this this User类只能访问“类用户”中的变量或对象和函数,不能访问“类dbinterface”中的变量或对象来访问已将该类扩展到User类的访问权限。你可以试试这个

class User extends dbinterface {

}

这会给你带来结果。