PHP - 编辑成功结果的类

时间:2015-03-28 22:01:53

标签: php

如何从3个类和1个类扩展ABCD结果中获取php数据。 有人可以帮忙修改我的代码吗?

class Common{
     public function __construct(){
          $this->data = 'A';
     }
     public function getData(){
          return $data;
     }
}

class SetOne extends Common{
     protected $data;
     public function __construct(){
          $this->data = 'B';
     }
}

class SetTwo extends Common{
     protected $data;
     public function __construct(){
          $this->data .= 'C';
          $obj = new SetOne();
     }
}

class SetTree extends Common{
     protected $data;
     public function __construct(){
          $this->data .= 'D';
          $obj = new SetTwo();
     }
}

$obj = new SetTree();
echo $obj->getData(); // I want to get the result: **ABCD**

我真的不知道怎么做。 :-(谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

一种解决方案是

class Common{
    protected $data;
    public function __construct(){
         $this->data = 'A';
    }
    public function getData(){
        return $this->data;
    }
}

class SetOne extends Common{
    public function __construct(){
        parent::__construct();
        $this->data .= 'B';
    }
}

class SetTwo extends SetOne{
    public function __construct(){
        parent::__construct();
        $this->data .= 'C';
    }
}

class SetTree extends SetTwo{
    public function __construct(){
        parent::__construct();
        $this->data .= 'D';
    }
}

$obj = new SetTree();
echo $obj->getData(); // I want to get the result: **ABCD**