Can you override interface methods with different, but “compatible”, signatures?

后端 未结 7 1272
长发绾君心
长发绾君心 2021-02-07 01:32

Consider the following PHP interfaces:

interface Item {
    // some methods here
}

interface SuperItem extends Item {
    // some extra methods here, not define         


        
7条回答
  •  Happy的楠姐
    2021-02-07 01:57

    As a workaround I'm using PHPDoc blocks in interfaces.

    interface Collection {
       /**
        * @param Item $item
        */
        public function add($item);
        // more methods here
    }
    
    interface SuperCollection extends Collection {
        /**
        * @param SuperItem $item
        */
        public function add($item);
        // more methods here that "override" the Collection methods like "add()" does
    }
    

    This way, in case you are properly using interfaces, IDE should help you catch some errors. You can use similar technique to override return value types as well.

提交回复
热议问题