如何在PHP中实现注册类(使用OOPS概念)?

时间:2011-02-16 08:06:45

标签: php class registration oop

我正在用PHP创建一个注册/注册页面,所以我创建了一个类class.register.php
因为我在PHP中实现了第一次完整的类基础方法,并且还使用了PDO。请建议我如何以优化的方式制作有价值的课程。以下是我的班级:

class.register.php

class registration 
{
    private $_confirm = 0;
    public $post_data = array();
    private $db;
    protected $db_name ='db_movies';
    protected $db_user = 'root';
    protected $db_pass ='keshav';


    function __construct($post_data)
    {
            $this->post_data = array_map('trim',$post_data);
            $this->db_connect();
            return $this->post_data;

    }

    protected function db_connect()
    {
        try {
                $this->db =  new PDO("mysql:host=localhost;dbname=$this->db_name", $this->db_user, $this->db_pass);     
                return $this->db;
            } 
        catch (PDOException $e) {
                print "Error!: " . $e->getMessage() . "<br/>";
                die();
            }
    }

    public function setPostIntoSession()
    {
        return $_SESSION['post_data']=$this->post_data;             
    }

    public function checkEmailInDB()
    {
      $stmt = $this->db->prepare("SELECT COUNT(*) FROM tbl_user WHERE email_address = :email");
      $stmt->bindParam(':email',$this->post_data['email_address'],PDO::PARAM_STR);
      $stmt->execute();
      $result = $stmt->fetchColumn();
      return $result;
    }

    public function insertIntoDB()
    {
      $qry = "INSERT INTO tbl_user SET 
                                            email_address = :email,
                                            first_name = :first_name,
                                            middle_initial = :middle_name,
                                            last_name = :last_name,
                                            title =:title,
                                            organization = : organization,
                                            primary_phone =:primary_phone,
                                            secondary_phone =:secondary_phone,
                                            website =:website,
                                            how_found =:found ";
      $stmt = $this->db->prepare($qry);
      $stmt->bindParam(':email',$this->post_data['email_address'],PDO::PARAM_STR);
      $stmt->bindParam(':first_name',$this->post_data['first_name'],PDO::PARAM_STR);
      $stmt->bindParam(':middle_name',$this->post_data['middle_initial'],PDO::PARAM_STR);
      $stmt->bindParam(':last_name',$this->post_data['last_name'],PDO::PARAM_STR);
      $stmt->bindParam(':title',$this->post_data['title'],PDO::PARAM_STR);
      $stmt->bindParam(':organization',$this->post_data['organization'],PDO::PARAM_STR);
      $stmt->bindParam(':primary_phone',$this->post_data['primary_phone'],PDO::PARAM_STR);
      $stmt->bindParam(':secondary_phone',$this->post_data['secondary_phone'],PDO::PARAM_STR);
      $stmt->bindParam(':website',$this->post_data['website'],PDO::PARAM_STR);
      $stmt->bindParam(':found',$this->post_data['how_found'],PDO::PARAM_STR);
      return $stmt->execute();
    }
}

这里我使用上面的课程

signup.php

<?php ob_start(); session_start();
include_once ('includes/class.register.php');
$register = new registration($_POST);
$fine = true;

$register->setPostIntoSession();
//echo "<pre>";
//print_r($register);

if(count($_POST)>0){    

        if($register->checkEmailInDB())
        {
            echo $_SESSION['error'] ='Sorry,given e-mail address already exists.';
            $fine = false;
        }
        if ($register->post_data['pswd'] !== $register->post_data['confirm_pswd'])
        { 
            echo $_SESSION['error'] ='Sorry,both password are not matching';
            $fine = false;          
        }
        else 
        {
            $register->insertIntoDB();
        }
}
if($fine == false ) 
{
    //header("location:signup.php");
    //exit();
}
else
{
    echo "All izz Well";
}

//$register->watch($register);
?>

我觉得我这么做很长,请告诉我应该在课堂上实现哪些功能。

1 个答案:

答案 0 :(得分:1)

免责声明:这是我的意见。 好吧我一个人喜欢按照MVC模式进行编码,并且还有适当的类继承。为什么我这样说,因为你似乎有很多东西混在一起(尽管代码是正确的)。对我来说,注册更多的是一个过程而不是一个模型对象,因此有一个模型类是不理想的。

这是我如何优化这个: 首先创建一个处理登录/连接和查询执行的通用DB类。 然后在创建模型时,从此类扩展,以便您可以使用这些方法。 例如,对于注册,它将是注册用户,因此假设您有一个用户表,您创建一个扩展db的用户类。在该用户类中,您可以处理输入验证并插入到DB中。 例如:

<?php
class ent_db
{
    private $DBUserName;
    private $DBPassword;
    private $DBHost;
    private $DBConnection = null;
    function __construct() 
    {
        $this->DBUserName = config::$dbuser;
        $this->DBPassword = config::$dbpass;
        $this->DBHost = config::$dbhost;
    }
    function executeQuery($sql)
    {
        return mysql_query($sql,$this->getConnection());

    }
     function getConnection()
    {
        if ($this->DBConnection===null)
            return mysql_connect($this->DBHost, $this->DBUserName,$this->DBPassword);
        else
            return $this->DBConnection;
    }
    function close()
    {
        if ($this->DBConnection!== null)
        {
            mysql_close($this->DBConnection);
            $this->DBConnection = null;
        }
    }
    function getAffectedRows()
{
    return mysql_affected_rows($this->getConnection());
}
function getAll($sql){

    $ret = $this->executeQuery($sql);
    if (mysql_num_rows($ret) !== 0)
    {
        while($row = mysql_fetch_assoc($ret))
        {
            $return[]=$row;
        }

        $this->close();
        return $return;

        }else
        {   
            $this->close();
            return false;
    }

        }
}
?>    

然后你可以拥有你的用户类(这里是它的结构)

    <?php
class ent_structures extends ent_db
{
function getStructures($sort=null)
{
    $sql = "SELECT * FROM crm.structure WHERE disabled = 0";
    $sql.=($sort!==null)?" ORDER BY $sort":"";
    $ret = $this->executeQuery($sql);
    //or return $this->getAll();
.
.
.    
.    

这会使事情变得清晰,并且每次都不允许您重写代码以连接到DB等。 然后我使用控制器处理流程,例如registerController来处理注册等。 使用辅助类来处理不属于任何一个类别的重复性任务。

我希望你明白这个想法。