PHP获取父类的变量

时间:2018-03-08 09:05:36

标签: php

我在PHP中尝试OO。我想我明白出了什么问题,但我不明白如何解决它。

class Name {
   public $_firstname;
   public $_lastname;

   public function setName($firstName, $lastName){
       $this->_firstname = $firstName;
       $this->_lastname = $lastName;                
   }

   public function getName(){
       echo 'The full name is '. $this->_firstname. ' ' . $this->_lastname .'<br>';
   }

}
class Description extends Name{
    public $_desciprion;

    public function setDescription($description){
        $this->_desciprion = $description;
    }
    public function getDescription(){
        echo $this->_desciprion. ' is written by '.$this->_firstname .'<br>';
    }
}

$firstNames = array("some", "another", "john");
$lastNames = array("body", "body", "doe");
$descriptions = array("description 1", "description 2", "description 3");



for ($i=0; $i < count($firstNames); $i++){
    $name = new Name();
    $name->setName($firstNames[$i], $lastNames[$i]);

    $description = new Description();
    $description->setDescription($descriptions[$i]);

    echo $description->getDescription();
}

我想回应包含$description类的$_firstname的{​​{1}}。

我真的不知道填写它的方法。

思想?

3 个答案:

答案 0 :(得分:4)

在您的情况下,for ($i=0; $i < count($firstNames); $i++){ $description = new Description(); $description->setName($firstNames[$i], $lastNames[$i]); $description->setDescription($descriptions[$i]); echo $description->getDescription(); } $name完全无关。但是,这很容易解决:

$description

奖金改善:

for ($i=0; $i < count($firstNames); $i++){
    $description = new Description();
    $description->setName($firstNames[$i], $lastNames[$i]);
    $description->setDescription($descriptions[$i]);

    echo $description->getDescription();
}

答案 1 :(得分:2)

sealed

答案 2 :(得分:0)

您要将名称设置为另一个实例($ name),以便将名称设置为您必须编写的描述对象:

xmlns="http://www.opengis.net/gml/3.2"