Is it possible to have an interface that has private / protected methods?

后端 未结 7 1122
逝去的感伤
逝去的感伤 2021-02-01 12:00

Is it possible in PHP 5 to have an interface that has private / protected methods?

Right now I have:

interface iService
{
    private method1();
}
         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 12:13

    Big NO, any method in the Interface will never have private or protected access identifier.

    **All methods declared in an interface must be public; this is the nature of an interface.

    Few other interesting facts about interface

    Interfaces can be extended like classes using the extends operator. They can extend only other interfaces. (source: https://www.php.net/manual/en/language.oop5.interfaces.php)

    Note that it is possible to declare a constructor in an interface, which can be useful in some contexts, e.g. for use by factories. Signature should be same in the child class.

    In your case, even another problem is - function keyword is missing in the function declaration. It should be

    interface iService
    {
        public function method1();
    }
    

提交回复
热议问题