title: PHP Object : Loop on product list
body: <p>I have a quick one: I'm a beginner with PHP Object, and I have a product table with all my products.</p>
  
  <p>I also have a class for my product (with a constructor that fills my instance with all the parameters of my product).</p>
  
  <p>Say I want a list of all my product, shall I create a separate class for my list of products, or create a function that creates a new instance of my product class ?</p>
  
  <p>I don't really get the benefits of it right now, because if I have to execute the exact same query as my class query, it's useless ?</p>
  
  <p>Thank's for your tips !</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thank's folks ! It's still quite complicated for me, but here's what I did (talking about books) trying to follow your advices.</p>
  
  <p>I created a DAO. Inside this DAO, I have two static functions that return me either a list of books or a single book, and one static that creates the book object.</p>
  
  <p>With this method, I still have 2 queries (one for the list, one for the single book). I have a feeling this is wrong but any help is appreciated :)</p>
  
  <pre><code>class bookDAO {
  
      public static function getBooks() {
          $books = array();
          $db = databaseConnection::getInstance();
          $query = $db-&gt;query("SELECT * FROM books");
          while($row = $query-&gt;fetchObject()) {
              $book = self::createBookFromRow($row);
              $books[] = $book;
          }
          return $books;
      }
  
      public static function getBook($id){
          $db = databaseConnection::getInstance();
          $query = $db-&gt;prepare("SELECT * FROM books WHERE id=:id");
          $query-&gt;execute(array(":id"=&gt;$id));
          $row = $query-&gt;fetchObject();
          $book = self::createBookFromRow($row);
          return $book;
      }
  
      private static function createBookFromRow($row) {
          $book = new book($row);
          return $book;
      }
  
  }
  </code></pre>