Question

Hello i have one class as child and one class as parent. One class again as Writer info.

I get an error when passing array of two objects (obj. from two class child) in writer class function. I get this :

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

My questions is :

  1. I have one child class that directly descendant of parent class. And i create method using Parent class type. Why not work?
  2. How to fix that?

This is my code :

<?php
    // Class Person
    class Person {
        public $name;
        public $gender;
        public $age;

        public function __construct($name, $gender, $age) {
            $this->name = $name;
            $this->gender = $gender;
            $this->age = $age;
        }

        public function getInfo() {
            $info = "Name : $this->name<br/>Gender : $this->gender<br/>Age : $this->age";   
            return $info;
        }
    }

    // Class Mahasiswa
    class Mahasiswa extends Person {
        public $npm;

        public function __construct($npm, $name, $gender, $age) {
            parent::__construct($name, $gender, $age);  
            $this->npm = $npm;
        }

        public function getInfo() {
            $info = "NPM : $this->npm<br/>";
            $info .= parent::getInfo();
            return $info;
        }
    }

    // Class PersonWriter
    class PersonWriter {
        private $persons = array();

        public function setData(Person $persons) {
            $this->persons[] =  $persons;
        }

        public function write() {
            $str = "";
            foreach ($this->persons as $person) {
                $str = "Name : $person->name<br/>";
                $str .= "Gender : $person->gender<br/>";
                $str .= "Age : $person->age<br/>";
            }

            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->setData($persons);
    $writer->write();
?>

Answer :

  1. In example code that checked as answer below.
  2. Oh okay, i catch it. I know understand my function setData(Person $persons) Because in my book not tell the explanation.

Now i add the person object to array like :

<?php ...
    $writer->setData($mhs);
    $writer->setData($mhs2);
?>

And i edited my function like this :

public function write() {
            $str = "";
            foreach ($this->persons as $person) {
                $str = "Name : $person->name<br/>";
                $str .= "Gender : $person->gender<br/>";
                $str .= "Age : $person->age<br/>";

                echo "<br/>";
                echo $str;
            }
        }

And it works now.

Was it helpful?

Solution

This needs a Person not an array;

$writer->setData($persons);

This will work;

$p = new Person("Jake", "male", 29);
$writer->setData($p);

This is because the setData function requires a Person object;

public function setData(Person $persons) {
    $this->persons[] =  $persons;
}

If you want it to accept an array do;

public function setData(array $persons) {
    $this->persons[] =  $persons;
}

Or you can make it accept anything you want, by removing the hint;

public function setData($persons) {
    $this->persons[] =  $persons;
}

EDIT

I am assuming this is not your code, because if it were it would be perfectly obvious why its broke when passing in an array. Please learn the basics of OO before diving in two feet first and struggling. The error makes it obvious what the issue is. Here it is explained;

// You are meant to pass in a Person object here, it then appends that
// to an existing array of Persons
public function setData(Person $persons) {
    $this->persons[] =  $persons;
}

// This array of Persons is then iterated over in this function, this is
// where line 48 is, 

public function write() {
    $str = "";
    // Here the foreach goes over the Persons array
    foreach ($this->persons as $person)
    {
        // But the $person objects are being accessed like objects using the
        // -> operator, so if you pass in an array it will fail because you do
        // no access an array using ->
        $str = "Name : $person->name<br/>";
        $str .= "Gender : $person->gender<br/>";
        $str .= "Age : $person->age<br/>";
    }

    echo $str;
}

You can change these lines to the following to access an array;

$str = "Name : " . $person['name'] . "<br/>";
$str .= "Gender : " . $person['gender'] . "<br/>";
$str .= "Age : " . $person['age'] . "<br/>";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top