本文介绍了PHP中的接口有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

允许您创建代码,该代码定义实现它的类的方法。但是,您无法向这些方法添加任何代码。

Interfaces allow you to create code which defines the methods of classes that implement it. You cannot however add any code to those methods.

允许你做同样的事情,以及向该方法添加代码。

Abstract classes allow you to do the same thing, along with adding code to the method.

现在如果你能用抽象类达到相同的目标,为什么我们甚至需要接口的概念?

Now if you can achieve the same goal with abstract classes, why do we even need the concept of interfaces?

我被告知它与OO理论有关,从C ++到Java,这是PHP的OO的基础。这个概念在Java中有用但在PHP中没有用吗?它只是一种避免在抽象类中散落占位符的方法吗?我错过了什么?

I've been told that it has to do with OO theory from C++ to Java, which is what PHP's OO stuff is based on. Is the concept useful in Java but not in PHP? Is it just a way to keep from having placeholders littered in the abstract class? Am I missing something?

推荐答案

接口的全部内容是让您可以灵活地让您的班级实施多个接口,但仍然不允许多重继承。从多个类继承的问题是多种多样的,并且页面总结了它们好吧。

The entire point of interfaces is to give you the flexibility to have your class be forced to implement multiple interfaces, but still not allow multiple inheritance. The issues with inheriting from multiple classes are many and varied and the wikipedia page on it sums them up pretty well.

接口是妥协。多重继承的大多数问题都不适用于抽象基类,所以现在大多数现代语言禁用多重继承,但是调用抽象基类接口,并允许类实现任意数量的那些。

Interfaces are a compromise. Most of the problems with multiple inheritance don't apply to abstract base classes, so most modern languages these days disable multiple inheritance yet call abstract base classes interfaces and allows a class to "implement" as many of those as they want.

这篇关于PHP中的接口有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:25
查看更多