何时在PHP中使用Interfaces [英] When to use Interfaces in PHP

查看:80
本文介绍了何时在PHP中使用Interfaces的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用PHP中的对象进行编码时,我总是很难理解接口的真正价值(可能是我想象的其他语言)

I have always had a hard time understanding the real value of Interfaces when coding with Objects in PHP (could be other languages I imagine)

据我了解你使用接口来强制或保证当Class使用接口时,该类将具有在该类中的接口

From what I understand you use an Interface to enforce or guarantee that when a Class is using an Interface that that class will have the methods defined in the Interface inside of that class.

因此,根据我对使用它们的小知识,这不意味着你在定义需要这些方法的1个以上的类时,才会发现接口有用吗?

So from my litte knowledge of using them, wouldn't that mean you would only find an Interface beneficial when defining more then 1 class that needs those Methods?

更清楚的是,如果我有一个类可以完成一件事,而其他类不需要做那样的事情,那么使用<是毫无意义的该类的code>接口?

To be more clear, if I have a class that does one thing and no other classes need to do that kind of thing, then it would be pointless to use an Interface on that class?

所以你不会使用接口在每个课上你是对吗?

So you wouldn't use an Interface on EVERY class you right?

PS)如果你把这个问题投票为完全重复那么你就没有了因为我已经阅读了大部分类似的问题,所以我只读了这个问题并且只读了标题。

PS) If you vote this question as exact duplicate then you didn't read the question and only the title as I have read most of the similar questions already

推荐答案


根据我的理解,你使用一个接口来强制执行或保证
,当一个Class使用一个接口时,该类将在该类的接口内定义
方法。

From what I understand you use an Interface to enforce or guarantee that when a Class is using an Interface that that class will have the methods defined in the Interface inside of that class.

这实际上只是交易的一半(技术部分)。当你使用界面时会出现一些非常重要的架构一半,它是这样的:

This is actually only half of the deal (the technical part). There's also the all-important architectural half, which appears when you consume the interface and it goes like this:

function feed(IAnimal $interface) {
    // ...
}

(或者,记录为返回实现 IAnimal 的实例的工厂函数也可作为示例)。

(alternatively, a "factory" function that is documented to return an instance that implements IAnimal would also serve as an example).

这里的想法是界面的消费者说:我想让动物喂食。我不在乎它是飞行,走路还是爬行。我不在乎它是否很大我只关心它与所有其他动物共享某些特征 - 包含接口定义的特征。

The idea here is that the consumer of the interface says: "I want an animal to feed. I don't care if it flies, walks, or crawls. I don't care if it's big or small. I only care that it shares some features with all other animals" -- features that would comprise the definition of the interface.

换句话说,接口用于从具体实现(类)中抽象出契约(接口)。这使得具体类的实现者可以自由地修改,重命名,删除和添加实现,而不会破坏接口用户的代码,如果您直接在API中引用具体类,这是不可能的。

In other words, interfaces serve to abstract the contract (interface) from the concrete implementation (classes). This gives implementers of concrete classes a free hand to modify, rename, remove and add implementations without breaking the code for users of the interface, something that is not possible if you are referencing concrete classes directly in your API.

至于仅由一个类实现的接口:这个信息不足以决定。如果将来可能有更多的接口实现,那么它确实有意义(例如:即使 IHashFunction 接口也会有意义> Sha1HashFunction 目前是唯一可用的实现。否则它不提供任何东西。

As for the interface that is implemented by one class only: that's not enough information to decide. If there can plausibly be more implementations of the interface in the future, then it certainly does make sense (for example: an IHashFunction interface would make sense even if Sha1HashFunction were currently the only available implementation). Otherwise it doesn't offer anything.

这篇关于何时在PHP中使用Interfaces的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆