title: How to passing array of objects in PHP OOP function
body: <p>Hello i have one class as child and one class as parent.
  One class again as Writer info.</p>
  
  <p>I get an error when passing array of two objects (obj. from two class child) in writer class function. I get this :</p>
  
  <pre><code>Catchable fatal error: Argument 1 passed to PersonWriter::setData() must be an instance of
  Person, array given, called in C:\xampp\htdocs\php\oop\book\Person.php on line 62 and
  defined in C:\xampp\htdocs\php\oop\book\Person.php on line 40
  </code></pre>
  
  <p>My questions is :</p>
  
  <ol>
  <li>I have one child class that directly descendant of parent class. And i create method using Parent class type. Why not work?</li>
  <li>How to fix that?</li>
  </ol>
  
  <p><strong>This is my code :</strong></p>
  
  <pre><code>&lt;?php
      // Class Person
      class Person {
          public $name;
          public $gender;
          public $age;
  
          public function __construct($name, $gender, $age) {
              $this-&gt;name = $name;
              $this-&gt;gender = $gender;
              $this-&gt;age = $age;
          }
  
          public function getInfo() {
              $info = "Name : $this-&gt;name&lt;br/&gt;Gender : $this-&gt;gender&lt;br/&gt;Age : $this-&gt;age";   
              return $info;
          }
      }
  
      // Class Mahasiswa
      class Mahasiswa extends Person {
          public $npm;
  
          public function __construct($npm, $name, $gender, $age) {
              parent::__construct($name, $gender, $age);  
              $this-&gt;npm = $npm;
          }
  
          public function getInfo() {
              $info = "NPM : $this-&gt;npm&lt;br/&gt;";
              $info .= parent::getInfo();
              return $info;
          }
      }
  
      // Class PersonWriter
      class PersonWriter {
          private $persons = array();
  
          public function setData(Person $persons) {
              $this-&gt;persons[] =  $persons;
          }
  
          public function write() {
              $str = "";
              foreach ($this-&gt;persons as $person) {
                  $str = "Name : $person-&gt;name&lt;br/&gt;";
                  $str .= "Gender : $person-&gt;gender&lt;br/&gt;";
                  $str .= "Age : $person-&gt;age&lt;br/&gt;";
              }
  
              echo $str;
          }
      }
  
      // Create 2 objects
      $mhs = new Mahasiswa("201143579091","Fandi Akhmad", "L", 21);
      $mhs2 = new Mahasiswa("201143579092","Annisya Ferronica", "P", 19);
  
      // Add objects to Array 
      $persons = array($mhs, $mhs2);
  
      $writer = new PersonWriter();
      $writer-&gt;setData($persons);
      $writer-&gt;write();
  ?&gt;
  </code></pre>
  
  <p>Answer :</p>
  
  <ol>
  <li>In example code that checked as answer below.</li>
  <li>Oh okay, i catch it. I know understand my function setData(Person $persons)
  Because in my book not tell the explanation.</li>
  </ol>
  
  <p>Now i add the person object to array like :</p>
  
  <pre><code>&lt;?php ...
      $writer-&gt;setData($mhs);
      $writer-&gt;setData($mhs2);
  ?&gt;
  </code></pre>
  
  <p>And i edited my function like this :</p>
  
  <pre><code>public function write() {
              $str = "";
              foreach ($this-&gt;persons as $person) {
                  $str = "Name : $person-&gt;name&lt;br/&gt;";
                  $str .= "Gender : $person-&gt;gender&lt;br/&gt;";
                  $str .= "Age : $person-&gt;age&lt;br/&gt;";
  
                  echo "&lt;br/&gt;";
                  echo $str;
              }
          }
  </code></pre>
  
  <p>And it works now.</p>