开发者

Interface and duck typing in dynamic languages

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:

  1. 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.
  2. 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜