Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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_Oop_Inheritance_Subclass - Fatal编程技术网

Php 使用父类中的子方法

Php 使用父类中的子方法,php,oop,inheritance,subclass,Php,Oop,Inheritance,Subclass,classes.php class System { protected $domain; public function __construct($domain) { $this->domain = $domain; } } class Subsystem extends System { public function __construct() { parent::__construct(); }

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转向面向对象的PHP,但我在理解继承的概念时遇到了一个问题。
为什么上面的代码不起作用?页面返回此错误:致命错误:未捕获错误:调用未定义的方法System::getDomain()继承不是这样工作的

创建实例时,类系统只有自己的方法和属性可用。而您的类子系统将拥有它自己的和它的父系统(系统)

有关更多信息,请参阅文档:
系统
类对子类的方法一无所知。因此不可能从父类使用子方法?实例化
子系统
而不是
系统
。孩子认识父母,但不是绝对不可能。谢谢,我会试试的。请随意写一个答案,这样我就可以标记为已回答。