我知道有很多关于 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 …Run Code Online (Sandbox Code Playgroud)