【问题标题】:php abstract class inheritance error with no abstract method to inheritphp抽象类继承错误,没有抽象方法可以继承
【发布时间】:2013-07-01 16:53:17
【问题描述】:

我是 OOP 的新手,我在学习练习时遇到了这个错误。

Class contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods

错误是从抽象类的子类抛出的,实现了一个接口。我知道抽象类的子类必须实现所有抽象方法,但我没有在父类或接口中声明任何抽象方法。如果我没有在子类中包含来自抽象类或接口的已声明抽象方法,我不应该只收到此错误吗?

子类:

class OuterViewDecorator extends AbstractViewDecorator
    {
    const DEFAULT_TEMPLATE = "/var/www/portfolio/simple-php/templates/layout.php";

    public function render() {
        $data["innerview"] = $this->view->render();
        return $this->renderTemplate($data);
    }
}

父类:

abstract class AbstractViewDecorator implements ViewInterface
{
    const DEFAULT_TEMPLATE = "default.php";
    protected $template = self::DEFAULT_TEMPLATE;
    protected $view;    

    public function __construct(ViewInterface $view)
    {
        $this->view = $view;
    }   

    public function render()
    {
        return $this->view->render();
    }   

    public function renderTemplate(array $data = array())
    {
        extract($data);
        ob_start();
        $template = include $this->template;
        return ob_get_clean($template);
    }
}

界面:

interface ViewInterface
{
    public function setTemplate($template);
    public function getTemplate();
    public function __set($field, $value);
    public function __get($field);
    public function __isset($field);
    public function __unset($field);
    public function render();
}

感谢您的帮助

【问题讨论】:

  • 从接口中删除 __set __get __isset 和 __unset 方法可能是安全的。

标签: php oop inheritance decorator abstract


【解决方案1】:

你是说它正在实现一个接口。

所有继承类之间必须实现所有接口方法 因此,例如,您的 AbstractViewDecorator 可以实现 2 个方法,OuterViewDecorator 可以实现最后 4 个,或者 OuterViewDecorator 可以实现所有 6 个。只要所有方法都是类继承链中的实现。

【讨论】:

  • 不一定非要/或在创建具体实例之前。例如,AbstractViewDecorator 实现其中一半,OuterViewDecorator 实现另一半是完全有效的。
  • lol 谢谢,没想到接口方法都需要用到
猜你喜欢
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多