PHP OOP代码运行良好。这是正确的做法吗?未来的一些发布会破坏它吗?

时间:2012-10-04 17:49:42

标签: php mysql oop

class lnemail_fetch

的代码段
<?php  Part of heritage_classes.php
// Declare classes
class lnemail_fetch {
// return string in format "title | factoid" 
    public  $result;
    public function get_ln_info() 
    {
    include ("./includes/LOheritage-config.php");
    mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
    mysql_select_db("$dbname") or die(mysql_error());
      $query = "SELECT * FROM lnemail";
     $result = mysql_query($query);
         $this->result = $result;
    }
}
?>

较大程序的代码片段它列出了一个MySQL表

    require_once('./includes/heritage_classes.php'); 

    $newlnemail_fetch = new  lnemail_fetch;
     $newlnemail_fetch->get_ln_info();
     $newresult  = $newlnemail_fetch->result;
     echo  "lnemail File display  <br />"; 

      while($row = mysql_fetch_array($newresult))
        {
         echo $row['ln_email']. "  |  " . $row['ln_date'] . "  |  " . $row['ln_week'] ;
          echo "<br />";

        }

这种PHP OOP的使用是否被认为是良好的做法,即使它现在很好用?

3 个答案:

答案 0 :(得分:0)

我会说不,这不是OOP的好用。

需要改进的地方:

分隔数据库连接和查询内容。 分离db结果处理。实现可迭代的结果对象将是一个好主意。 不使用mysql扩展并切换到mysqli是一个非常好的主意。它还将免费为您提供MySQL的OOP接口。

可能会考虑在SQL字符串中转义输入的方面,但这是不可判定的,因为没有显示这样的代码。

答案 1 :(得分:0)

  

未来有哪些版本会破坏它吗?

是的,因为您使用旧的和(现在)已弃用的mysql_* functions

  

课程lnemail_fetch的代码段

名称lnemail对于一个班级来说并不是一个好名字,因为当我看到它时,我不知道ln的含义。类名通常是UpperCamelCased和方法camelCased

现在来看看你的代码:

在查看你的课时,它只是一个课程,目前与OOP无关。我要做的是将$result属性设为私有,因为目前您只是数据的容器。此外,我还将介绍另一个类,它将负责访问数据库中的数据(或您拥有的任何存储)。我还将介绍另一个类来表示单个电子邮件和工厂类来构建这些邮件对象。这看起来如下所示:

//不确定收件箱是否是正确的名称,因为我不太清楚该类代表什么

class Inbox
{
    private $storage;

    private $mailFactory;

    public function __construct($storage, $mailFactory)
    {
        $this->storage     = $storage;
        $this->mailFactory = $mailFactory;
    }

    public function fetchAllMails()
    {
        $mailRecordset = $this->storage->fetchAll();

        $mails = array();
        foreach ($mailRecordset as $mailRecord) {
            $mails[] = $this->mailFactory->create($mailRecord);
        }

        return $mails;
    }
}

class InboxStorage
{
    private $dbConnection;

    public function __construct(\PDO $dbConnection)
    {
        $this->dbConnection = $dbConnection;
    }

    public function fetchAll()
    {
        $stmt = $this->dbConnection->query('SELECT * FROM lnemail');

        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
}

class Email
{
    private $email;

    private $date;

    private $week;

    public function __construct($email, $date, $week)
    {
        $this->email = $email;
        $this->date = $date;
        $this->week = $week;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getDate()
    {
        return $this->date;
    }

    public function getWeek()
    {
        return $this->week;
    }
}

class EmailFactory
{
    public function create($record)
    {
        return new Email($record['email'], $record['date'], $record['week']);
    }
}

你可以像下面那样运行它:

// initialize all the objects we are going to need
$emailFactory = new EmailFactory();
$dbConnection = new \PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$inboxStorage = new InboxStorage($dbConnection);
$inbox = new Inbox($inboxStorage, $mailFactory);

// run the code
foreach($inbox->fetchAllMails() as $email) {
    echo $mail->email . ' | ' . $mail->date . ' | ' . $mail->week . '<br>';
}

答案 2 :(得分:-1)

这不是一个真正的类,因为lnemail_fetch不是一个对象。您所做的只是创建一个容器,并且必须使容器只调用一个可能是静态的函数并返回结果而不是分配它。

更好的类可能包括较新的mysqli而不是过时的mysql,并按如下方式工作。它使行成为对象,列为属性(变量;

<?php
class lnemail {
    public $ln_emai;
    public $ln_date;
    public $ln_week;

    public static function Fetch($dbhost,$dbuser,$dbpass,$dbname) {
      $db = new mysqli($dbhost, $dbuser, $dbpass,$dbname) or die(mysql_error());
      $query = "SELECT * FROM lnemail";
      $result = $db->query($query);
      $returnArr = array();
      while($obj = $result->fetch_object('lnemail') {
         $returnArr[] = $obj;     
      }
      return $returnArr;
    }
}

然后

 <?php
 require_once("./includes/LOheritage-config.php");
 require_once('./includes/heritage_classes.php');
 $lnemails = lnemail::Fetch($dbhost,$dbuser,$dbpass,$dbname);
 echo  "lnemail File display  <br />";
 foreach($obj as $lnemail) {
      echo $obj->ln_email. "  |  " . $obj->ln_date . "  |  " . $obj->ln_week;
      echo "<br />";
 }