PHP 过程式到 OOP

发布于 2024-11-29 21:29:58 字数 2308 浏览 1 评论 0原文

我正在尝试将程序代码转换为 oop。

 <?php
 $dbc = get_dbc();
 $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
 while ($info_row = mysqli_fetch_array($info))
 {
      $info_id = $info_row['info_id'];
      $info_title = $info_row['info_title'];
 ?>
 <div style="width: 100%;">
      <div style="float: left;">
           <?php echo $info_id; ?>
      </div>
      <div style="float: left;">
           <?php echo $info_title; ?>
      </div>
      <div style="clear: both;"></div>
 </div>
 <?php } ?>

我对没有 HTML 样式的类/对象的不完整尝试:

 <?php
 class InfoTest {

      private $info_id;
      private $info_title;

      public function __construct() {
           $dbc = get_dbc();
           $info = $dbc->query ("SELECT info_id, info_title FROM text");
           if ($dbc->error) {
                printf("Error: %s\n", $dbc->error);
           }       
           while ($info_row = $info->fetch_array())
           {
                $info_id = $info_row['info_id'];
                $info_title = $info_row['info_title'];  
           }
           $info->free();
           $this->info_id = $info_id;
           $this->info_title = $info_title; 
      }


      public function setInfoID() {
           $this->info_id = $info_id;
      }

      public function getInfoID() { 
           return $this->info_id;
      }

      public function setInfoTitle() {
           $this->info_title = $info_title;
      }

      public function getInfoTitle() {
           return $this->info_title;
      }

      public function __destruct() {    
      }

 }

 ?>
 <?php
 $display = new InfoTest();
 echo $display->getInfoID();
 echo $display->getInfoTitle();
 ?>

我的程序代码打印出:1 一 2 二。

我的 oop 代码打印出: 2 二

根据我的理解,oop 是这样打印的,因为 $info_id 和 $info_title 不是数组,并且只打印出最后存储的信息。

因此,如果我将: 更改

$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];

为:

$info_id[] = $info_row['info_id'];
$info_title[] = $info_row['info_title'];

并打印数组,它会显示我想要的所有信息,但如何以非数组形式显示它?

到目前为止我所做的事情是正确的还是我的做法是错误的?

I'm trying to convert my procedural code to oop.

 <?php
 $dbc = get_dbc();
 $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
 while ($info_row = mysqli_fetch_array($info))
 {
      $info_id = $info_row['info_id'];
      $info_title = $info_row['info_title'];
 ?>
 <div style="width: 100%;">
      <div style="float: left;">
           <?php echo $info_id; ?>
      </div>
      <div style="float: left;">
           <?php echo $info_title; ?>
      </div>
      <div style="clear: both;"></div>
 </div>
 <?php } ?>

My incomplete attempt at classes/objects without the HTML styling:

 <?php
 class InfoTest {

      private $info_id;
      private $info_title;

      public function __construct() {
           $dbc = get_dbc();
           $info = $dbc->query ("SELECT info_id, info_title FROM text");
           if ($dbc->error) {
                printf("Error: %s\n", $dbc->error);
           }       
           while ($info_row = $info->fetch_array())
           {
                $info_id = $info_row['info_id'];
                $info_title = $info_row['info_title'];  
           }
           $info->free();
           $this->info_id = $info_id;
           $this->info_title = $info_title; 
      }


      public function setInfoID() {
           $this->info_id = $info_id;
      }

      public function getInfoID() { 
           return $this->info_id;
      }

      public function setInfoTitle() {
           $this->info_title = $info_title;
      }

      public function getInfoTitle() {
           return $this->info_title;
      }

      public function __destruct() {    
      }

 }

 ?>
 <?php
 $display = new InfoTest();
 echo $display->getInfoID();
 echo $display->getInfoTitle();
 ?>

My procedural code prints out: 1 One 2 Two.

My oop code prints out: 2 Two

From my understanding the oop prints out that way because $info_id and $info_title aren't arrays, and only print out the last stored information.

So, if I change:

$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];

To:

$info_id[] = $info_row['info_id'];
$info_title[] = $info_row['info_title'];

And print the arrays, it displays all the information I want, but how to display it in non-array form?

Is what I'm doing so far correct or am I approaching this wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尴尬癌患者 2024-12-06 21:29:58

你做错了。在您的程序示例中,您一次迭代一行数据;在面向对象的示例中,如果将它们视为数组然后打印它们,那么您将一次浏览一列数据。我不会将数据分成单独的 id 和标题,而是将它们视为一个捆绑包(即类似于您在程序版本中的做法)- id 与标题一起使用,而不是其他 id,对吗?

因此,例如,您可能有一个成员变量

private $texts = array();

,然后在构造函数中执行以下操作:

while ($info_row = $info->fetch_array()) {
    $text = array(
        'id' => $info_row['info_id'],
        'title' => $info_row['info_title']
    );
    $this->texts[] = $text;
}

然后提供一个方法来获取此数组数组:

public function getTexts() {
    return $this->texts;
}

最后,您可以对其进行迭代,与过程示例中的操作非常相似:

<?php
$display = new InfoTest();
foreach ($display->getTexts() as $text) {
    ?>
    <!-- html goes here -->
    <?php echo $text['info_id']; ?>
    <!-- more html -->
    <?php echo $text['info_title']; ?>
    <!-- other html -->
    <?
}
?>

退后一步——你可能会问这一切是否真的有必要。过程式 PHP 本质上并没有什么问题——如果它做了您需要它做的事情并且做得很清楚,那么在这里您可能会更好地选择简单而不是复杂。

You're doing it wrong. In your procedural example you're iterating over the data a row at a time; in your OO example, if you treat them as arrays and then print them, you're going through the data a column at a time instead. Rather than separating the data into separate ids and titles, I would treat them as a bundle (i.e. similar to how you did it in the procedural version) - an id goes with a title, not other ids, right?

So, for example, you might have a member variable

private $texts = array();

and then in your constructor, do:

while ($info_row = $info->fetch_array()) {
    $text = array(
        'id' => $info_row['info_id'],
        'title' => $info_row['info_title']
    );
    $this->texts[] = $text;
}

and then provide a method to get at this array of arrays:

public function getTexts() {
    return $this->texts;
}

Finally, you could iterate over it very similarly to how you did in the procedural example:

<?php
$display = new InfoTest();
foreach ($display->getTexts() as $text) {
    ?>
    <!-- html goes here -->
    <?php echo $text['info_id']; ?>
    <!-- more html -->
    <?php echo $text['info_title']; ?>
    <!-- other html -->
    <?
}
?>

Stepping back - you could ask if all this is really necessary. There's nothing inherently wrong with procedural PHP - if it does what you need it to do and does it clearly, you might be better off favoring simple over complex here.

站稳脚跟 2024-12-06 21:29:58

因为 info id 是对象中的一个数组,所以获取该值的相应函数应该采用偏移量。或者更好的是,您应该查看实现迭代器的类,以便您可以对对象执行 foreach

Because info id is an array in your object the corresponding function to get the value should take an offset. Or even better you should look at your class implementing iterator so that you can just do foreach over your object

余罪 2024-12-06 21:29:58

在切换到 OOP 之前,我首先将代码模块化并开始分割成逻辑彼此分离的功能部分,例如数据库访问和模板化:

<?php

/**
 * infos provider
 *
 * @return array
 */
function get_infos()
{
    $infos = array();
    $dbc = get_dbc();
    $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
    while ($info_row = mysqli_fetch_array($info))
    {
        $infos[] = (object) $info_row;
    }
    return $infos;
}

foreach(get_infos() as $info)
{
?>
<div style="width: 100%;">
     <div style="float: left;">
          <?php echo $info->info_id; ?>
     </div>
     <div style="float: left;">
          <?php echo $info->info_title; ?>
     </div>
     <div style="clear: both;"></div>
</div>
<?php } ?>

然后将与数据库相关的功能移动到它自己的文件中,以将其与“模板”。完成后,您可以考虑进一步的重构步骤。我建议阅读以下内容(仅独立于命名框架): 当扁平 PHP 遇到 symfony

Before switching to OOP I would first of all modularize the code and start to segment into functional parts that have separated logic from each other, e.g. database access and templating:

<?php

/**
 * infos provider
 *
 * @return array
 */
function get_infos()
{
    $infos = array();
    $dbc = get_dbc();
    $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
    while ($info_row = mysqli_fetch_array($info))
    {
        $infos[] = (object) $info_row;
    }
    return $infos;
}

foreach(get_infos() as $info)
{
?>
<div style="width: 100%;">
     <div style="float: left;">
          <?php echo $info->info_id; ?>
     </div>
     <div style="float: left;">
          <?php echo $info->info_title; ?>
     </div>
     <div style="clear: both;"></div>
</div>
<?php } ?>

Then move the database related functions into a file of it's own to decouple it from the "templates". After that's done you can think about further steps to refactor. I suggest the following read (which is merely independent to the named framework): When flat PHP meets symfony.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文