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

时间:2014-02-08 09:42:08

标签: php oop type-hinting

你好我有一个班作为孩子,一个班作为父班。 一个班级再次作为作家信息。

在writer类函数中传递两个对象(来自两个类子对象)的数组时出错。我明白了:

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. 如何解决这个问题?
  3. 这是我的代码:

    <?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) 因为在我的书中没有说明解释。
    3. 现在我将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;
                  }
              }
      

      它现在有效。

1 个答案:

答案 0 :(得分:2)

这需要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/>";