【问题标题】:Is it suitable to use an abstract class in this situation php?在这种情况下使用抽象类是否适合php?
【发布时间】:2014-03-08 05:02:49
【问题描述】:

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

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

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 上的抽象类。如果它不合适,我应该做些什么来避免两个类具有基本相同的代码。感谢您的帮助。

【问题讨论】:

    标签: php oop


    【解决方案1】:

    是的,但是您的代码远非面向对象的设计,因为您的类做了很多事情:

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

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

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

    【讨论】:

    • 好的,谢谢您的输入。如果我把它分成几个类会更好吗?如果有,你会推荐什么?
    • 是的,它会更好,但我只能向您展示一些主要准则,例如将数据库连接内容放在单独的 claas 中并使用 PDO,将 HTML 代码放入视图模板等等。 Fox 是一个完整的解决方案,我们需要了解您正在使用的整个基础架构,并且您需要深入了解 OO 编程。
    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 2011-06-16
    • 2014-03-25
    相关资源
    最近更新 更多