PHP - 使用子类继承。使用子类

时间:2016-09-08 23:26:05

标签: php class inheritance

我正在尝试更改这两个值,'首先'并且'最后'名称,使用子类。我的教科书只显示了如果主类中只有一个变量它是如何工作的。我试图扩展它,并且不得不猜测在使用多个变量/值时它是如何完成的。我尝试过这方面的变体,并通过浏览器通过服务器进行检查。

<?php
class contactInfo {
    public$first = "Robert";
    public$last = "Heston";
    public$address = "3289 Westmore Blvd.";
    public$city = "Greensboro";
    public$state = "NC";
    public$zip = "27128";

/ *书中的例子中有一个变量与我的第一个变量相同。并且它表示该功能必须与该类具有相同的名称,并且它具有等同于&#34; $ ft&#34;在括号中使用&#34; $ this-&gt; first = $ ft;&#34;用它。我尝试扩展它,这个东西是不好的。 * /

    function contactInfo($ft, $lt, $as, $cy, $se, $zp) {
        $this->first = $ft;
        $this->last = $lt;
        $this->address = $as;
        $this->city = $cy;
        $this->state = $se;
        $this->zip = $zp;
    }
    function showMixedInfo() {
    echo "My name is ".$this->first." ".$this -> last."<br/>";
    echo "I live at ".$this ->address."<br/>";
    echo "and my city and state are".$this->city." ".$this->state."<br/>";
    echo "and my zip code is".$this->zip."<br/>";
}
}

/ *我创建了子类和变量,这是我知道的唯一方法。在书中,&#39; $ newInfo = new childClass()&#39;在括号中更改了实际名称,但我需要更改2个值。 * /

class childClass extends contactInfo {
    //Maybe there needs to be some code here?
}
$newInfo = new childClass();
$this->first("Graham");
$this->last("Rouse");
$newinfo -> showMixedInfo();
?>

教练&#34;提示&#34;使用了这样的子类,我不知道如何创建其他所有内容,而不是上面的内容:

class childClass extends contactInfo {

function showMixedInfo($ft, $lt) {
    $this->first = $ft;
    $this->last = $lt;
    echo "My name is ".$this->first." ".$this -> last."<br/>";
    echo "I live at ".$this ->address."<br/>";
    echo "and my city and state are".$this->city." ".$this->state."<br/>";
    echo "and my zip code is".$this->zip."<br/>";
}
}

我对这个感到茫然。该指导员提供超出教科书范围的作业,然后在没有重读/学习的数量显示它时坚持在那里。我之前已经搜索过解决方案。

0 个答案:

没有答案