简体   繁体   中英

Can't call parent method properties from inheritance class in php

I'am begginer in OOP PHP. I have code like this

class Index 
{
    public $activepage = true;
    public $url;
    public $page;

    function __construct()
    {
        if ($this->activepage) {
            $this->url = "Yes";
            $this->page = "Home";
        } else {
            $this->url = "No";
            $this->page = "Index";
        }

    }


    public function show()
    {
        return $this->page;
    }


    public function showTest()
    {
        return "test";
    }
}

class Home extends Index
{

    function __construct()
    {
        echo $this->show();
    }
}

$page = new Home;

My questions is : Why I have blank page when I invoke Home class?

But when I change constructor in Home class like this echo $this->showTest(); , it works. and displaying "test" on screen.

and what actually diferrent between my show method and showTest method in Index class?

When you add a __construct() in the Home class it overrides the construct from the parent class Index .

You can invoke the parent construct manually with:

function __construct()
{
    parent::__construct();
    echo $this->show();
}

您可以像这样调用Parent类方法:

parent::show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM