开发者

PHP Procedural To OOP

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?


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜