title: Whats the best way to create an array of objects inside a class as attribute in PHP?
body: <p>So I have hard time figuring out the best way and the correct way to do this, so I'm posting this question if someone can help me here, as I'm doing a Laravel project in PHP.</p>
  <p>So lets imagine we are building an app for books, each book have several pages, some have 20 and some might have 700, if we were to create a class, or classes for these books correctly, how do we do that?</p>
  <p>Below I have few ways to go about it, but I don't know what would work, and/or what is the best one.</p>
  <p>Option 1:</p>
  <pre><code>&lt;?php
  namespace Translation/Logic;
  
  public class Book {
    // atributes
    public $title;
    public $year;
    public $author;
    public $description;
    public $date;
    public $language = &quot;English&quot;;
  
    // as an atribute maybe we need to have an array of pages here? like
    // public $pages = Pages[]??
  }
  
  public class Pages {
     public number;
     public orientation;
     public direction;
     public page;
     public chapter;
     public content;
  
  }
  </code></pre>
  <p>Option 2:</p>
  <pre><code>&lt;?php
  namespace Translation/Logic;
  
  abstract class Book {
    // atributes
    public $title;
    public $year;
    public $author;
    public $description;
    public $date;
    public $language = &quot;English&quot;;
  
    // as an attributes maybe we need to have an array of pages here? 
  }
  
  public class Pages extends Book {
     public number;
     public orientation;
     public direction;
     public page;
     public chapter;
     public content;
  
  }
  // Then create an array of Pages here, but then we have all the attributes of the books in every page, which don't really make sense?
  </code></pre>
  <p>I'm pretty sure I'm going about it the wrong way here, and there is a smarter way to do this, but just need a little help here.</p>
  <p>If it is one of my solution, what is the code that should complete my code, also is there any alternative ways other than mine, and what will be the pros and cons of each one.</p>
  <p>If not and I'm wrong, what is then the correct way to define these classes, and why?</p>
  <p>Thanks for taking the time to answer me :)</p>