title: Models in mvc (best practices, PHP)
body: <p>I know there are plenty of articles and questions about MVC and best practices, but I can't find a simple example like this: </p>
  
  <p>Lets say I have to develop a web application in PHP, I want to do it following the MVC pattern (without framework).
  The aplication should have a simple CRUD of books.</p>
  
  <p>From the controller I want to get all the books in my store (that are persisted in a database).</p>
  
  <p>How the model should be?</p>
  
  <p>Something like this:</p>
  
  <pre><code>class Book {
      private $title;
      private $author;
  
      public function __construct($title, $author)
      {
          $this-&gt;title = $title;
          $this-&gt;author = $author;
      }
  
      public function getTitle()
      {
          return $this-&gt;title;
      }
  
      public function setTitle($title)
      {
          $this-&gt;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;
      }
  .
  .
  .
  </code></pre>
  
  <p>So I call it(from the controller) like that:</p>
  
  <pre><code>$book_service = new BooksService();
  $all_books = $book_service-&gt;getBooks();
  $one_book = $book_service-&gt;getOneBook('title');
  </code></pre>
  
  <p>Or maybe should be better have everything in the Books class, something like this:</p>
  
  <pre><code>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-&gt;title = $title;
          $this-&gt;author = $author;
      }
  
      public function getTitle()
      {
          return $this-&gt;title;
      }
  
      public function setTitle($title)
      {
          $this-&gt;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;
      }
  .
  .
  .
  </code></pre>
  
  <p>So I call it(from the controller) like that:</p>
  
  <pre><code>$book_obj = new Book();
  $all_books = $book_obj-&gt;getBooks();
  $one_book = $book_obj-&gt;getOneBook('title');
  </code></pre>
  
  <p>Or maybe I'm totally wrong and should by in a very different way?</p>
  
  <p>Thank you!</p>