e.g. in dynamic language such as PHP, since we have duck typing, is 开发者_开发知识库Interface only a constraint, it has no use in practice?
There are two cases I can think of which give it some practical importance:
- If desired, an interface allows you to behave like a faux static typed language: you can check the runtime type of an object to see if it implements the interface, and you can also get PHP to do it for you automatically with type hinting.
- It documents the relationships between type hierarchies (in a big library). This is a big practical gain for a human trying to understand said hierarchies.
精彩评论