Abstract function parameter type hint overriding in PHP 7

后端 未结 2 779
有刺的猬
有刺的猬 2020-12-20 17:01

Is it possible to override an abstract function in PHP 7 with a function in a child class that would narrow down on the accepted argument type?

A word of elaboration

2条回答
  •  天涯浪人
    2020-12-20 17:16

    Looks like no pretty solutions so we can try use interfaces.

    OR

    interface DeliveryTargetAction {}
    
    
    class AbstractDeliveryTarget {
    
    }
    
    class EmailDeliveryTarget   extends  AbstractDeliveryTarget implements DeliveryTargetAction {
    
    }
    
    
    abstract class Q {
         abstract protected function action(DeliveryTargetAction $class);
    }
    
    class Y extends Q {
        protected function action(DeliveryTargetAction $class){}
    }
    

    Depends on what you want to do.

提交回复
热议问题