简体   繁体   English

如何在PHP OOP函数中传递对象数组

[英]How to passing array of objects in PHP OOP function

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. 在writer类函数中传递两个对象(obj。来自两个类子)的数组时出错。 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. 我使用Parent类类型创建方法。 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. 我知道了解我的函数setData(Person $ persons)因为在我的书中没有说出解释。

Now i add the person object to array like : 现在我将person对象添加到数组中,如:

<?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. 它现在有效。

This needs a Person not an array; 这需要一个Person而不是一个数组;

$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; 这是因为setData函数需要Person对象;

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. 在先用双脚潜水并挣扎之前,请先了解一下OO的基本知识。 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/>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM