0

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.

3
  • 1
    Read the error message. You are hinting a Person instance in the method, but want to pass an array. Commented Feb 8, 2014 at 9:46
  • Yes i understand for that error message, and i want to pass array of objects in function. How to do that? Commented Feb 8, 2014 at 9:49
  • By passing an array. And not hinting instance of Person. Commented Feb 8, 2014 at 9:52

1 Answer 1

2

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/>";
Sign up to request clarification or add additional context in comments.

3 Comments

No, it not work. I already try that. But i get this error : ( ! ) Notice: Trying to get property of non-object in C:\xampp\htdocs\php\oop\book\Person.php on line 48 . I just want to pass array of objects in my function.
Tried what? I gave you three different approaches. Using setData(Person $persons) means you cannot pass an array. So change that, then you can pass an array.
Yes i already try your 3 solutions before ask. And i get that error. Trying to get property of no-object...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.