-1

我使用 php 大约 4 年了,但我仍然没有在我自己的任何脚本上真正使用 OOP 方法。不过,我真的很想使用它,因为就我所听到的和我所理解的而言,它使开发变得更加容易。例如下面的代码,它是一个简单的 mysql-select,获取类别,从结果中我正在构建一个表单元素,一个下拉菜单,其中涉及一些步骤,比如 fe 也使用 natcasesort() 对内容进行排序。Atm 我会为国家重做相同的程序,我会再次编写相同的代码,只是将类别替换为国家,如您所见:

$result1 = mysql_query("SELECT * from categories WHERE hidden = 0");

while ($row = mysql_fetch_array($result1)) { 
    $cat_names = explode(",",$row['title_mulilingual']);
    $categories[$row['uid']] = $cat_names[$lang_id];
}

natcasesort($categories);
foreach ($categories as $key => $value) {
    if ($key == $active_key) $selected = ' selected="selected"';
    else $item_selected = '';
    $select_fields['cat_id'] .= '<option'.$selected.' value="'.$key.'">'.$value.'</option>';
}   

$cat_select = '<select name="category" >'.$select_fields['cat_id'].'</select>';


// the following part is the same as the first one, it only handles countries instead of categories

$result2 = mysql_query("SELECT * from countries WHERE hidden = 0");

while ($row = mysql_fetch_array($result2)) { 
    $country_names = explode(",",$row['title_mulilingual']);
    $countries[$row['uid']] = $country_names[$lang_id];
}

natcasesort($countries);
foreach ($countries as $key => $value) {
    if ($key == $active_key) $item_selected = ' selected="selected"';
    else $selected = '';
    $select_fields['country_id'] .= '<option'.$selected.' value="'.$key.'">'.$value.'</option>';
}   

$country_select = '<select name="country" >'.$select_fields['country_id'].'</select>';

所以在代码中它显然不是 OOP。但我猜,我可以通过使用 OOP 更轻松地做到这一点,对吧?有人可以通过使这个示例成为 OOP 示例来帮助我更好地理解 OOP 吗?在此先感谢,杰登

4

1 回答 1

2

有人可以通过使这个示例成为 OOP 示例来帮助我更好地理解 OOP 吗?

嘿,您可以使用 MVC 之类的框架轻松地将其移植到 OOP 中。

MVC结构
(来源:php-html.net

“模型-视图-控制器 (MVC) 是一种用于计算机用户界面的设计模式,它将应用程序划分为三个责任区域:

  • 模型:表示应用程序状态的域对象或数据结构。
  • 视图,它观察状态并向用户生成输出。
  • 控制器,它将用户输入转换为模型上的操作。”(维基百科,2012)

一些非常好的 OOP 框架已经可用。

可以在此处找到有关如何创建模型视图控件的好教程。

控制器

这会检索数据,然后将其发送到要处理的模型。

 include_once("model/Model.php");

 class Controller {
 public $model; 

 public function __construct()
 {
      $this->model = new Model();
 } 

 public function invoke()
 {
      if (!isset($_GET['book']))
      {
           // no special book is requested, we'll show a list of all available books
           $books = $this->model->getBookList();
           include 'view/booklist.php';
      }
      else
      {
           // show the requested book
           $book = $this->model->getBook($_GET['book']);
           include 'view/viewbook.php';
      }
 }
 }

模型

它处理要呈现的数据

include_once("model/Book.php");

class Model {
public function getBookList()
{
    // here goes some hardcoded values to simulate the database
    return array(
        "Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."),
        "Moonwalker" => new Book("Moonwalker", "J. Walker", ""),
        "PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "")
    );
}

public function getBook($title)
{
    // we use the previous function to get all the books and then we return the requested one.
    // in a real life scenario this will be done through a db select command
    $allBooks = $this->getBookList();
    return $allBooks[$title];
}

}

class Book {
public $title;
public $author;
public $description;

public function __construct($title, $author, $description)
{
    $this->title = $title;
    $this->author = $author;
    $this->description = $description;
}
}

看法

呈现数据。

<?php 

    echo 'Title:' . $book->title . '<br/>';
    echo 'Author:' . $book->author . '<br/>';
    echo 'Description:' . $book->description . '<br/>';

?>

根据我的经验,我发现使用框架进行 OOP 比在 Web 应用程序中从头开始学习它更容易。

还有一个关于在 nettuts 上创建自己的框架的很好的教程,可以在这里找到,如果您不理解上述信息,本教程不会让人不知所措。

于 2012-05-22T21:42:57.040 回答