2017-09-06 139 views
0

這是抽象視圖從2類繼承

<?php 
abstract class Abstract_View 
{ 
    abstract function render($name); 
} 
?> 

這是在我的控制器類視圖

class View extends Abstract_View 
{ 
function render($name) 
{ 
    require __DIR__.'/../views/header.php'; 
    require __DIR__.'/../views/'.$name.'.php';//jaye $name masaln miad index/index 
    require __DIR__.'/../views/header.php'; 
} 
} 

i。從視圖類instatiate其他類使用視圖類繼承從控制器類

<?php 
class Controller 
{ 
function __construct() 
{ 
    $this->view = new View(); 
} 
} 

我爲索引控制器創建了一個抽象類

<?php 
abstract class Abstract_Index 
{ 
abstract function index(); 
} 
?> 

,這是指數:

<?php 
class Index extends Controller 
{ 
function __construct() 
{ 
    parent::__construct(); 
} 
public function index(){ 
    $this->view->render('index/index'); 
} 
} 

,我的問題是,我必須從控制器繼承了使用對象視圖和我有繼承的形式摘要索引以及如何從兩個類中繼承,這是正確的?

+1

查找到的性狀(http://php.net/manual/en/language.oop5.traits.php) –

+1

您可以參考這裏 - https://stackoverflow.com/a/13966131/7789884 – chad

+3

可能的[與接口的PHP多繼承]重複(https://stackoverflow.com/questions/13966054/php-multiple-inheritance-with-interfaces) –

回答

0
abstract class Abstract_Index extends Controller 

class Index extends Abstract_Index 

這應該工作。

還刪除構造函數,因爲它只是調用父構造函數。沒有構造函數意味着父對象將默認被調用。

或者,由於index()方法是抽象的,也許只是使它成爲一個接口?

<?php 

interface IndexableInterface 
{ 
    public function index(); 
} 

然後執行它。

<?php 

class Index extends Controller implements IndexableInterface 
{ 
    public function index() 
    { 
     // etc 
    } 
}