簡體   English   中英

如何在PHP OOP函數中傳遞對象數組

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

您好我有一個班級作為孩子,一個班級作為家長。 一個班級再次作為作家信息。

在writer類函數中傳遞兩個對象(obj。來自兩個類子)的數組時出錯。 我明白了:

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

我的問題是:

  1. 我有一個子類,它是父類的直接后代。 我使用Parent類類型創建方法。 為什么不工作?
  2. 如何解決?

這是我的代碼:

<?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();
?>

答案:

  1. 在下面檢查為答案的示例代碼中。
  2. 哦,好吧,我抓住它。 我知道了解我的函數setData(Person $ persons)因為在我的書中沒有說出解釋。

現在我將person對象添加到數組中,如:

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

我編輯了我的功能:

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;
            }
        }

它現在有效。

這需要一個Person而不是一個數組;

$writer->setData($persons);

這將有效;

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

這是因為setData函數需要Person對象;

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

如果你想讓它接受一個數組呢;

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

或者你可以通過刪除提示讓它接受你想要的任何東西;

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

編輯

我假設這不是你的代碼,因為如果它是非常明顯的,為什么它在傳遞數組時破壞了。 在先用雙腳潛水並掙扎之前,請先了解一下OO的基本知識。 該錯誤使問題顯而易見。 在這里解釋;

// 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;
}

您可以將這些行更改為以下內容以訪問數組;

$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