Do PHP interfaces have properties?

前端 未结 2 1152
死守一世寂寞
死守一世寂寞 2021-02-11 11:57

Do interfaces in PHP have properties, or do they only have methods?

相关标签:
2条回答
  • 2021-02-11 12:38

    You can declare properties in DocBlock for the interface. IDE's will then hint those properties for the interface (PhpStorm does) but this will not force the actual implementation of these fields in the implementing class. E. g.

    /**
     * @property string $password
     * @property string $username
     */
    interface IUserDocument
    {
    
    
    }
    
    0 讨论(0)
  • 2021-02-11 12:44

    It depends what you mean by "properties". If you mean actual fields, then no, they don't. If you're referring to properties such as those in C#, then yes they can (since the property accessors are strictly syntactic sugar for accessor methods anyway). The same goes for events (though of course, in each case, no implementation is specified for the get/set or add/remove accessors).

    Update: Since PHP does not have properties in the sense of get/set accessors, then the answer to your question is no. Interfaces cannot carry their own data/state.

    0 讨论(0)
提交回复
热议问题