将PHP脚本重建为面向对象的编程

时间:2014-09-24 21:41:28

标签: php oop

我正在尝试将login_1.php重建为OOP,但我没有成功。在login_1.php中,$ numberofrows正在被回显但是在login_2.php中我没有回复或错误。我不知道我的错误在哪里。如果我注释掉getData方法,那么我得到错误“未定义方法的调用”。我没有看到login_2.php中的getData方法与login_1.php中的流程有很大的区别

login_1.php(可行):

<form action="login.php" method="post" name="login">
   <input type="text" name="username" /><br />
   <input type="password" name="password" id="password"/><br />
   <input type="submit" value="Login" name="login" />
</form>
<?php
       $mysqli = new mysqli('localhost', 'root', '****', '******');
       if(isset($_POST['login'])){

        $username = $_POST['username'];
        $password = $_POST['password'];
        echo $username;
        $stmt = $mysqli->prepare("SELECT username, password FROM users WHERE username=? AND  password=? LIMIT 1");
        $stmt->bind_param('ss', $username, $password);
        $stmt->execute();
        $stmt->bind_result($username, $password);
        $stmt->store_result();
        $numberofrows = $stmt->num_rows();
        echo  '# rows: '.$numberofrows;

}
?>

的LoginController:

<?php

     error_reporting(-1);
     ini_set('display_errors', 1);


       if (isset($_POST['login'])) {

                 $username = $_POST['username'];
                 $password = $_POST['password'];
                if(!empty($username) && !empty($password)){

                   try{
                   include "./models/login_2.php";
                   $login = new Login($username, $password);

                   if ($login== TRUE) {
                     session_start();
                     $_SESSION['username'] = $username;
                     header('Location: index.php');
                   }


                    }catch(Exception $exc ){
                   echo $exc->getMessage();
                    }
              }else{ echo "Please enter your username und your password!";}
            }

  ?>

login_2.php(getData方法不会回显$ numberofrows)

<?php

class Login
{
    private $host        = "localhost";
    private $user        = "root";
    private $db_password = "***";
    private $database    = "****";

    private $db;

    private $username;
    private $password;

    function __construct($username, $password)
    {
        // Set data
        $this->setData($username, $password);

        // connect to db
        $this->connectToDb();

        //get Data
        $this->getData();

    }

   private function setData($username, $password){

             $this->username = $username;
             $this->password = $password;

            }

    private function connectToDb(){
                      session_start();
                      // connect to the server.
                   $this->db = new mysqli($this->host, $this->user, $this->db_password, $this->database);    
                       if ($this->db->connect_errno) {
                           die("We are sorry, you could not be connected to the server,
                            plaese check your connection setting!");
                       }else{
                           echo "You are connected to the database";
                       }


            }

       private function getData(){
               $stmt = $this->db->prepare("SELECT username, password FROM users WHERE username=? AND  password=? LIMIT 1");

               $stmt->bind_param('ss', $username, $password);
               $stmt->execute();
               $stmt->bind_result($username, $password);
               $stmt->store_result();
               $numberofrows = $stmt->num_rows();
               echo  '# rows: '.$numberofrows;

           }
   }
?>

我感谢任何帮助。

0 个答案:

没有答案