1

代码片段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 是否被认为是一种好的做法,即使它现在工作得很好?

4

3 回答 3

0

我会说不,它没有很好地使用 OOP。

需要改进的方面:

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

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

于 2012-10-04T17:54:25.657 回答
0

将来的版本会破坏它吗?

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

类的代码片段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>';
}
于 2013-02-27T21:03:44.807 回答
-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 />";
 }
于 2012-10-04T19:09:11.523 回答