简体   繁体   English

抽象成员函数无法访问继承类的属性

[英]abstract member functions fail to access atributes of inheriting class

in php I have this code. 在PHP中,我有此代码。 I'm trying to get an inherited method to utilize a member variable of its child class. 我正在尝试获取一个继承方法来利用其子类的成员变量。

abstract class HtmlObj{
//abstract protected function jQuery_Activity();
public $hyperlink;
abstract protected function php_Activity();
abstract protected function print_Widget();

function __construct($hyperlink=""){
    if(isset($hyperlink)){
        $this->hyperlink = $hyperlink;
    }
    $this->php_Activity();
    $this->Print_Widget();
}

} }

class child extends HtmlObj{
   public $id;
   protected function php_Activity(){return;}
   protected function print_Widget(){
      print $this->id;
   }
   function __construct($id){
     this->id = $id;
   }
}

unfortunately this prints nothing. 不幸的是,这不会打印任何内容。 any insights as to why? 为什么有任何见解?

in child class You need to reffer to parent::__construct() by doing something like 在子类中,您需要通过做类似的事情来引用parent :: __ construct()

abstract class HtmlObj
{
//abstract protected function jQuery_Activity();
    public $hyperlink;

    abstract protected function php_Activity();

    abstract protected function print_Widget();

    function __construct($hyperlink = "")
    {
        if (isset($hyperlink)) {
            $this->hyperlink = $hyperlink;
        }
        $this->php_Activity();
        $this->Print_Widget();
    }
}

class child extends HtmlObj
{
    public $id;

    protected function php_Activity()
    {
        return;
    }

    protected function print_Widget()
    {
        print $this->id;
    }

    function __construct($id)
    {
        $this->id = $id;
        parent::__construct();
   }
}

new child(10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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