在这种情况下,php是否适合使用抽象类?

时间:2014-03-08 05:02:50

标签: php oop

我已经为用户登录和用户注册创建了一个类。它们非常相似,唯一的区别是类登录只为用户创建一个表单来输入电子邮件和密码,然后根据数据库检查密码,同时注册类让用户输入电子邮件,密码,名字和姓氏,将这些写入数据库。

例如,这是我的简单登录类:

class Login {
private $email;
private $password;

private $server;
private $username;
private $database_password;
private $database;

function __construct( $server, $username, $database_password, $database) {
    $this->server = $server;
    $this->username = $username;
    $this->database_password = $database_password;
    $this->database = $database;

    $this->init('email');
    $this->init('password');
}

private function init($value) {
    if(isset($_POST[$value])) {
        $this->$value = $_POST[$value];
    }
    else {
        $this->$value = null;
    }
}

public function checkFormValues() {
    if($this->email== null || $this->password == null) {
        if($this->email != null || $this->password != null ) {
            echo "<p>Please fill out all the fields</p>";
        }

        echo "
        <p>Please complete the form below</p>
        <form action=\"user_login.php\" name=\"login\" method=\"post\">
            email    :<input type=\"text\" name=\"email\" value=$this->email ><br>
            password :<input type=\"password\" name=\"password\" value=$this->password ><br>
            <input type=\"submit\" value=\"submit\" />
        </form>
        ";
        }

    else {
        $mysqli = new mysqli( $this->server, $this->username, $this->database_password, $this->database );
        $db = $mysqli->select_db( $this->database );
        $query = "SELECT password FROM Users WHERE emailAddress='$this->email'";
        $users_password = $mysqli->query($query);
        $row = $users_password->fetch_row();

        if($row[0] == md5($this->password))
            echo "correct";

        else
            echo "incorrect";
    }

  } 
}

以下是我的注册类:

class Register {

private $firstname;
private $lastname;
private $email;
private $password;

private $server;
private $username;
private $database_password;
private $database;


function __construct( $server, $username, $database_password, $database ) {
    $this->server = $server;
    $this->username = $username;
    $this->database_password = $database_password;
    $this->database = $database;

    $this->init('firstname');
    $this->init('lastname');
    $this->init('email');
    $this->init('password');    

    }

private function init($value) {
    if(isset($_POST[$value])) {
        $this->$value = $_POST[$value]; 

    }
    else {
        $this->$value = null;
    }

}

public function checkFormValues() {
    if((($this->firstname == null) || ($this->lastname == null)) || (($this->email == null) || ($this->password == null))) {
        if((($this->firstname != null) || ($this->lastname != null)) || (($this->email != null) || ($this->password != null))) {
            echo "<p>Please fill out all the fields</p>";
        }

        echo "
        <p>Please complete the form below</p>
        <form action=\"register_class.php\" name=\"register\" method=\"post\">
            First Name:<input type=\"text\" name=\"firstname\" value=$this->firstname ><br >
            Last Name: <input type=\"text\" name=\"lastname\" value=$this->lastname ><br>
            email    :<input type=\"text\" name=\"email\" value=$this->email ><br>
            password :<input type=\"password\" name=\"password\" value=$this->password ><br>
            <input type=\"submit\" value=\"submit\" />
        </form>
        ";
        }

    else{

    $mysqli = new mysqli( $this->server, $this->username, $this->database_password, $this->database );
    $db = $mysqli->select_db( $this->database );
    $query ="INSERT INTO Users(firstName, lastName, emailAddress, password) VALUE('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['email']."', '".md5($_POST['password'])."')"; 
    $mysqli->query($query);
    $mysqli->close();
    }
}

}

在这种情况下,使用抽象类是个好主意。我是OOP的新手,并在php.net上阅读抽象类。如果不合适,我应该做些什么来避免让两个类具有基本相同的代码。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

是的,但是你的代码远非面向对象的设计,因为你的课程做得很多:

  • 处理CRUD操作
  • 使用全局变量
  • 创建HTML输出

现在你的UI对MySQL数据库有很强的依赖性,但为什么会这样?

您应该阅读并了解有关OO含义的更多信息,例如SOLID原则。