开发者

Interface and duck typing in dynamic languages

开发者 https://www.devze.com 2023-02-24 17:27 出处:网络
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 practi

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

精彩评论

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