【问题标题】:inherit from 2 class从2类继承
【发布时间】:2017-09-06 11:08:56
【问题描述】:

这是抽象视图

<?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';
}
}

在我的控制器类中,我从视图类实例化到其他类以使用从控制器类继承的视图类

<?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');
}
}

我的问题是我必须从 Controller 继承才能使用对象视图,我必须继承抽象索引的形式以及如何从两个类继承,这是正确的吗?

【问题讨论】:

标签: php inheritance


【解决方案1】:
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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2018-05-23
    • 1970-01-01
    • 2015-12-21
    • 2015-07-01
    相关资源
    最近更新 更多