Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 扩展类无法从父类获取数据_Php_Class_Subclass - Fatal编程技术网

Php 扩展类无法从父类获取数据

Php 扩展类无法从父类获取数据,php,class,subclass,Php,Class,Subclass,我试图在扩展类中使用一个display函数,它首先在父类中获取一个display函数。但是,它不会在echo语句中显示变量。游戏类型(在本例中为“一天”)不显示 <?php class Cricket { protected $gameType; function __construct($gameType) { $this->gameType=$gameType; } function display() {

我试图在扩展类中使用一个display函数,它首先在父类中获取一个display函数。但是,它不会在echo语句中显示变量。游戏类型(在本例中为“一天”)不显示

<?php
class Cricket
{
    protected $gameType;

    function __construct($gameType)
    {
        $this->gameType=$gameType;
    }

    function display()
    {
        echo 'The cricket match is a ' . $this->gameType . " match";
    }
}

class Bowler extends Cricket
{
    public $type;
    public $number;

    function __construct($type,$number)
    {
        $this->type=$type;
        $this->number=$number;

        parent::__construct($this->gameType);
    }

    function display()
    {
        parent:: display();
        echo " with " . $this->number . " " . $this->type . " bowler";
    }
}   

$one = new Cricket("day-night");
$one->display();

echo'<br>';

$two  = new Cricket("day-night");
$two = new Bowler("left-hand","2");
$two->display();
?>

实例化Bowler类的过程实际上会发生变化,正如调用父构造函数
父构造函数所暗示的那样::\u construct(),创建一个全新的板球类以及保龄球类

因此,试图访问这个新创建的Cricket类的属性是没有意义的

因此,当您实例化
Bowler
类时,您还必须传递Cricket类成功构建所需的任何数据

比如说

<?php
class Cricket
{
    protected $gameType;

    function __construct($gameType)
    {
        $this->gameType=$gameType;
    }

    function display()
    {
        echo 'The cricket match is a ' . $this->gameType . " match";
    }
}

class Bowler extends Cricket
{
    public $type;
    public $number;

    function __construct($gameType, $type, $number)
    {
        $this->type=$type;
        $this->number=$number;

        parent::__construct($gameType);
    }

    function display()
    {
        parent:: display();
        echo " with " . $this->number . " " . $this->type . " bowler";
    }
}   

$two = new Bowler('day-night', "left-hand","2");
$two->display();

$this->gameType
传递给父类是没有用的,父类已经可以访问它了。不知道
一天的
应该从哪里来,但是看起来您想将
$this->type
传递给父类的构造函数,而不是
$this->gameType
$two=new Cricket()
$two=新保龄球手()<代码>$two
一次只能是一件事。[只能有一个:)]好的,就是只能有一个(我得到了),我如何让最后一个显示器显示以下内容,但仍然使用partent display功能。“板球比赛为期一天,有两名左手投球手参加。”。