【问题标题】:Models in mvc (best practices, PHP)mvc 中的模型(最佳实践,PHP)
【发布时间】:2015-10-30 09:07:24
【问题描述】:

我知道有很多关于 MVC 和最佳实践的文章和问题,但我找不到像这样的简单示例:

假设我必须用 PHP 开发一个 Web 应用程序,我想按照 MVC 模式(没有框架)来做。 应用程序应该有一个简单的书籍 CRUD。

我想从控制器获取我商店中的所有书籍(保存在数据库中)。

模型应该是怎样的?

类似这样的:

class Book {
    private $title;
    private $author;

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

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
        return this;
    }   
.
.
.


class BooksService{

    public getBooks(){
        //get data from database and return it

        //by the way, what I return here, should by an array of Books objects?
    }

    public getOneBook($title){
        //get data from database and store it into $the_title, $the_autor
        $the_book = new Book($the_title, $the_autor);
        return $the_book;
    }
.
.
.

所以我(从控制器)这样称呼它:

$book_service = new BooksService();
$all_books = $book_service->getBooks();
$one_book = $book_service->getOneBook('title');

或者最好将所有内容都放在 Books 类中,如下所示:

class Book 
{
    private $title;
    private $author;

    //I set default arguments in order to create an 'empty book'...
    public function __construct($title = null, $author = null)
    {
        $this->title = $title;
        $this->author = $author;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
        return this;
    }   

    public getBooks(){
        //get data from database and return it

        //and hare, what I should return? an Array?
    }

    public getOneBook($title){
        //get data from database and store it into $the_title, $the_autor
        $the_book = new Book($the_title, $the_autor);
        return $the_book;
    }
.
.
.

所以我(从控制器)这样称呼它:

$book_obj = new Book();
$all_books = $book_obj->getBooks();
$one_book = $book_obj->getOneBook('title');

或者也许我完全错了,应该以非常不同的方式?

谢谢!

【问题讨论】:

    标签: php model-view-controller


    【解决方案1】:

    看看你的两个解决方案,问问自己 - 如果你想开始添加越来越多的查找类型(按作者、标题、日期)会发生什么。他们应该属于书本模型吗? (model 是一本书的表示)。不会是我的答案。

    Book 类应该是您的 Book(标题、页数、文本等)的表示形式 - 在这种情况下存储在数据库中。

    在我看来,您的第一种方法是最好的方法 - 将查找 Book 的业务逻辑分离到一个单独的对象中(按照您的约定,我建议使用类似 BookLookupService 的东西),该对象负责查找 book 实例,例如:

    interface IBookLookupService {
        findBookByTitle($bookTitle);
        findBooksByAuthor($authorName);
        getAllBooks();
    }
    

    【讨论】:

    • 实现IBookLookupService和使用BookService类有什么区别?作者已经分出了两类,一类是书本是Book,一类是业务逻辑是BookService
    • @TomSawyer 因为您没有引用具体的实现,这意味着您可以轻松地将您的实现替换为 IBookLookupService 以进行模拟(例如)。您应该始终致力于针对接口而非实现进行编程。
    【解决方案2】:

    两种方式都是正确的,对应两种不同的模式。

    您的第一个想法可能与 Data mapper pattern 相关联。 在这种情况下,您的书本类不必了解有关数据库的任何信息,而让另一个类担心数据库操作。 这是 Doctrine2 或 Hibernate 等项目使用的模式(我认为)

    第二个称为 Active record pattern,您将所有内容保存在一个类中,并且您的 book 对象必须管理自己的持久性。 这是 Ruby on Rails 使用的模式。

    第一种方法通常为您提供更大的灵活性,但第二种方法可能看起来更直观且易于使用。

    一些信息:

    http://culttt.com/2014/06/18/whats-difference-active-record-data-mapper/

    【讨论】:

    • 第二种方式:在Book对象中存储getBooks之类的方法会导致内存占用或将不必要的方法暴露给公众?图片你有一个 bookArray 包含 100 本书,每本书都有相同的 getBooksgetOneBook 方法
    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 2011-09-16
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多