使用来自父项的子方法 class

Use child method from parent class

classes.php

class System {

    protected $domain;

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

}

class Subsystem extends System {

    public function __construct() {
        parent::__construct();
    }

    public function getDomain() {
        echo $this->domain;
    }

 }

index.php

require('classes.php');

$system = new System('http://google.com');
$system->getDomain();

我最近决定从面向过程转向面向对象PHP,但我在理解继承的概念时遇到了问题。 为什么上面的代码不起作用?页面 returns 此错误:致命错误:未捕获错误:调用未定义的方法 System::getDomain()

继承不是这样工作的。

您的 class 系统在创建实例时只有自己的方法和属性可用。虽然您的 Class 子系统将拥有自己的所有子系统及其父系统(系统)。

有关详细信息,请查看文档: http://php.net/manual/en/language.oop5.inheritance.php