从程序代码转移到oop

时间:2015-06-09 18:30:19

标签: php ajax json oop

我相对较新的编程,大约1年的兼职学习经验,我是非常新的PHP,只有几周,并使用w3schools来帮助我

我现在正试图切换到OOP风格,并且无法理解这个概念,或者更确切地说是如何以及何时应用它并因此转向这里,我已经很长一段时间潜伏而不是贡献者提供一些帮助和建议

我想要一些帮助/简短解释如何以PHP oop方式编写以下内容

$sql="SELECT name, lastname, member_nr, joindate, order, creditCardInfo
 from members 
 WHERE member_nr = $'member_nr'";
$result = mysql_query($sql) or die("error");
 while($row = mysql_fetch_array){
  echo '<h3>Personal Profile</h3>';
 echo  $name = $row['name'];
  echo $lastName = $row['lastname'];
 :
 :
 }

我的问题是我不知道我是否应该为上面创建一个班级,个人档案可以被视为一个人的权利,这是一个对象......?但是我是否在我的班级中包含了HTML部分和mysql_query部分等...我在OOP时遇到了很多问题,很难自己学习这就是为什么我真的很感激任何一点帮助或建议我可以上这里

谢谢

2 个答案:

答案 0 :(得分:2)

首先,您应该使用mysqli,因为所有mysql_*都已弃用,并且将来会从PHP中删除。

这里是一个带有一些功能的基础类,但你应该扩展它以供你使用!并了解自己没有评论的功能,了解更多信息。 这使得与数据库的多个连接成为可能,所以阅读一些关于单身的东西对你也有好处!

<?php
class dbconnection {
    private $dbHostname;
    private $dbName;
    private $dbUsername;
    private $dbUserpassword;

    private $error;
    private $querysuccess;
    private $connected;

    private $mysqli;

    public function __construct() {
        $this->dbHostname = 'localhost';
        $this->dbName = 'databasename';
        $this->dbUsername = 'username';
        $this->dbUserpassword = 'password';
        $this->connected = false;
    }
    public function __destruct() {
        $this->mysqli->Close();
    }

    public function connect() {
        //establishing a database connection
        $this->mysqli = new mysqli($this->dbHostname, $this->dbUsername, $this->dbUserpassword, $this->dbName);
        if($this->mysqli->connect_errno) {//When there was an error during the connecting
            echo 'Connection Error:<br>'.$this->mysqli->connect_error;
            $this->connected = false;
        } else {
            $this->connected = true;
        }
        if(!$this->mysqli->set_charset('utf8')) {//Need to be done for some functions!
            echo 'Error during seting the charset: <br>'.$this->mysqli->error;
        }
    }

    public function doquery($query_str) {
        //method which executes the query
        $returnval = false;
        if($this->connected && $query_str != '') {
            //only when connected AND the query is not empty
            $res = $this->mysqli->query($query_str);//this is the equivalent of mysql_query
            if($this->error_get() == '') {//Check for an error
                $this->querysuccess = true;
            } else {
                $this->querysuccess = false;
            }
            $returnval = $res;
        } else {
            echo 'No database connection, this needs some!';
        }
        return $returnval;
    }
    public function query_success() {
        return $this->querysuccess;
    }
    public function affected_rows_get() {
        return $this->mysqli->affected_rows;
    }
    public function error_get() {
        return $this->mysqli->error;
    }
    public function insert_id_get() {
        return $this->mysqli->insert_id;
    }
    public function escape_str($str) {
        return $this->mysqli->real_escape_string($str);
    }
    public function is_connected() {
        return $this->connected;
    }
    public function fetch_assoc(&$res){
        return $res->fetch_assoc();
    }
    public function fetch_assoc_array(&$res){
        return $res->fetch_array(MYSQLI_ASSOC);
    }
}

答案 1 :(得分:1)

这种情况不是一个好例子。当您想要封装一个实体的逻辑以简化其在类外的使用时,您需要一个类。

例如:

class Member {
   private $id, $name, 
           $lastname, $member_nr, $joindate, $order, $creditCardInfo;
   public function __construct($id = 0) {
        if($id != null) $this->loadFromDB();
   }

   private function loadFromDB() {
       //Do db load here...
   }

   public function Update(){
      //Do db update
   }

   public function Delete() {
      //Do db delete...
   }

   public function GetFromMemberNR($nr) {
        //Do select
   }

   //Additional Functions Verfication
}

最后,您将拥有相当复杂的代码。但要在外面使用它只需要包含类的php文件。

include 'member.php';
$member = new Member();
$member->GetFromMemberNR(2); //Does everything for you inside the function..

有很多工具可以帮助您更轻松地进行数据库操作,但这是OOP的基础。主要思想是封装可重用性。