dongrong1856 2016-12-29 13:17
浏览 78
已采纳

PHP。 是否有可能将非抽象方法重新声明为抽象方法?

Key class has a non-abstract method getDescription() (for many purposes).

Key class is extended by (one of) "a child abstract class".

"A child abstract class" is extended by "a concrete class".

I want to force "a concrete class" to implement getDescription() method and try to redeclare it in "a child abstract class" as an abstract method, but fail.

So, is it possible to redeclare non-abstract method as abstract in php? If not - how to force a concrete method to be implemented?

The key class. I need getDescription as declared in this way for many purposes.

abstract class beverage{
public $description = "Unknown beverage";
public function getDescription(){return $this->description;}
};

The abstract class, overriden getDescription() - but does not work.

abstract class condimentDecorator extends beverage {
    //public abstract function getDescription();
};

Concrete class, which MUST reimpliment getDescription.

class milk extends condimentDecorator{
public $beverage;
public function __construct(beverage $beverage){$this->beverage = $beverage;}
public function getDescription(){return $this->beverage->getDescription().", Milk";}
};
  • 写回答

2条回答 默认 最新

  • dongwei1263 2016-12-29 14:00
    关注

    is it possible to redeclare non-abstract method as abstract in php?

    No, you can't do that in PHP


    It seems that you are trying to implement a Decorator pattern.
    In such case the presented classes chain is over-complicated.
    Decorator pattern is dedicated to add(extend) functionality to another class without changing its structure.
    The following is a classical and more optimal approach of implementing decorators:

    abstract class Beverage {
        public $description = "Unknown beverage";
    
        public function getDescription(){
            return $this->description;
        }
    }
    
    class CondimentDecorator extends Beverage {
        protected $beverage;
    
        public function __construct(Beverage $beverage) {
            $this->beverage = $beverage;
        }
    
        public function getDescription() {
            return "Description:\t" . $this->beverage->getDescription() . " with condiment";
        }
    };
    
    class Milk extends Beverage {
        public function __construct($desc = "") {
            $this->description = $desc;
        }
    }
    
    $milk = new Milk('Milk beverage');
    $milk_with_condiment = new CondimentDecorator($milk);
    echo $milk_with_condiment->getDescription();
    

    The output:

    Description:    Milk beverage with condiment
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决