何时在 PHP 中使用 extends/abstract 和 Implements/interface

发布于 2025-01-05 11:51:41 字数 523 浏览 0 评论 0原文

所以,我开始对 PHP 中的抽象接口概念感到惊讶。
但什么时候它才真正有用呢?

当然,我可以使用界面为我的类制定规则,因此它们都遵循特定的模式。 但是什么时候它真正有用?

为什么我应该创建一个抽象类,而不是仅仅创建一个自己工作但对其他类有用的类。
摘要我也许可以仔细思考一下,发现一个很好的用途,例如通过开设一个普通课程。就像创建一个抽象数据库类,然后将其扩展为 Mysql 和 MsAccess 数据库类。提供两种相似的功能,在两种情况下都能提供无缝体验。

但实际上,有人能给我一个更好的例子来说明抽象接口何时真正有用吗?
请注意,我确实知道它是如何工作的,或者如何编写代码,只是不知道如何或何时使用它。

谢谢!

So, I am starting to gasp the concept of abstract and interface in PHP.
But when is it really ever useful?

Sure I can with an interface make up the rules for my classes, so they all follow a specific pattern. But when is it really useful?

And why should I make an abstract class instead of just making a class that works by it own but is useful for other classes.
Abstract I perhaps can put my head around and see a good use, for example by making a general class. Like making a abstract Database class, then extending it onto a Mysql- and a MsAccess database class. Giving both similar functions to work with, allowing seamless experience in both cases.

But really, could anyone give me a better example of when abstract and interface are really useful?
And please note, I do know how it works, or how to write the code, just not how or when to use it.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

合久必婚 2025-01-12 11:51:41

摘要的意思是“这是您的课程的模式,以及一些可以帮助您开始的代码”。抽象类的设计者可以将一些方法标记为需要由扩展类(abstract)提供,或者标记为final,这意味着该类不能重写它们。

接口的意思是“这是你的类的模式,但你必须自己编写所有代码”。接口中声明的任何方法和属性都必须由实现该接口的类提供。

因此,基本上的经验法则是:

如果您的代码可以或必须由后代类使用,则需要一个抽象类。如果只有方法和属性声明,则可以使用接口。

不要忘记,使用抽象类会在某种程度上限制您,因为后代类只能扩展一个类,而它可以实现任意数量的接口。

Abstract means "here is a pattern for your class, and some code that can start you off". The designer of the abstract class can mark some methods as needing to be provided by the extending class (abstract), or as final, which means the class can't override them.

Interface means "here is a pattern for your class, but you have to write all the code yourself". Any methods and properties declared in the interface must be provided by the class that implements the interface.

So essentially a rule of thumb will be:

If you have code that can or must be used by the descendant classes, you need an abstract class. If you only have method and property declarations, you can use an interface.

Don't forget, using abstract classes limits you to some extent, because a descendant class can only extend one class, whereas it can implement any number of interfaces.

装纯掩盖桑 2025-01-12 11:51:41

接口不是类,而抽象类已经是类。

由于每个类都有一个接口(根据定义),接口允许您在任何类(无论是否抽象)的旁边指定一个接口。

然后,针对接口进行编程允许您将一个类替换为另一个类,同时保持相同的接口。所以它会降低你的代码的耦合度。它不再针对具体类进行编程,而是“仅”针对更轻量级的接口进行编程。如果您不想针对具体的类名而是针对类型进行编程,那么接口会很有用。然后,您可以将某个对象替换为实现相同接口的任何其他对象。

另一方面,抽象类——甚至称为抽象——是非常具体的。但是,它不是最终的,因此它形成了类的模式,就像规范如何为特定功能编写从其扩展的类一样。由于它是抽象的,如果不从它延伸出来,它就无法生存。抽象类用于创建基类,其代码将被多次使用,以减少代码重复且无法直接实例化。

An interface is not a class, while an abstract class is already a class.

As every class has an interface (by definition), the interface allows you to specify an interface next to any class, be it abstract or not.

Programming against interfaces then allows you to replace one class with another one while keeping the same interface. So it makes your code less coupled. It's not programmed against concrete classes any longer but "only" against more lightweight interfaces. An interface then is useful if you don't want to program against concrete class names but against types. You can replace an object then with any other object that implements the same interface.

An abstract class on the other hand - even called abstract - is pretty concrete. However, it's not final, so it forms a pattern of a class, like a specification how a class that extends from it should be written for a certain functionality. As it's abstract, it can't come to live without extending from it. Abstract classes are used to create base classes with code that will be used more than once to reduce code duplication and not being able to instantiate directly.

沫离伤花 2025-01-12 11:51:41

接口就像合同一样工作。如果一个类实现了一个接口,那么使用该类的其他代码现在就知道它支持某些功能。

PHP 中接口的最佳示例是 Iterator 接口。

接口的好处是类可以实现多个。 “扩展”不允许这样做。这意味着子类可以实现接口,但其父类不必这样做。

阅读设计模式。您会发现其中的很多内容都已涵盖,并且您将永远不会再怀疑在哪些情况下它是有意义的。我认为《Head First Design Patterns》是一本好书,写得非常好。尽管他们在示例中使用 Java,但大多数内容都非常适用于 PHP。

我还有一个现实生活场景。

每当发生错误时,我们的应用程序总是到处抛出异常。每种类型的异常都有其自定义类。 RecordNotFound 异常就是一个例子。

如果未捕获异常,则会有一个顶级异常块,看起来有点像这样:

try {

 // Do everything in the app!

} catch (Exception $e) {

  // draw a good error page

}

在某些情况下,需要将异常映射到某些 HTTP 状态代码,例如 404(未找到)。因此我们有这个接口:

interface HTTPException {

  function getHTTPCode();

}

任何异常,无论继承树有多深,现在都可以实现这个接口并发出特定的 HTTP 状态代码。

我什至创建了接口,没有任何方法。我将让你尝试思考为什么这可能是有意义的。

An interface works like a contract. If a class implements an interface, other code that uses this class now knows it supports certain features.

The best example of an interface in PHP is the Iterator interface.

The benefit of interfaces is that classes can implement multiple. 'Extending' does not allow this. This means that a subclass can implement an interface, but it's parent doesn't have to.

Read up on design patterns. A lot of this you'll find covered, and you'll never have doubt again in which cases it makes sense. 'Head first design patterns' I thought to be a good book, very nicely written. Although they use Java in their examples, most stuff is very much applicable in PHP.

I have one more real-life scenario.

Our application always throws exceptions everywhere whenever an error occurs. Every type of exception gets its custom class. An example of this is a RecordNotFound exception.

If exceptions are not caught, there's a top-level exception block, which looks a bit like this:

try {

 // Do everything in the app!

} catch (Exception $e) {

  // draw a good error page

}

In some cases exceptions need to be mapped to certain HTTP status codes, such as 404 (not found). Therefore we have this interface:

interface HTTPException {

  function getHTTPCode();

}

any exception, no matter how deep in the inheritance tree can now implement this interface and emit a specific HTTP status code.

I even created interfaces, without any methods. I'll leave it to you to try to think of a reason why that may makes sense.

So尛奶瓶 2025-01-12 11:51:41

接口只不过是类的骨架结构,它不是类。我们可以实现该类并添加一些额外的功能以使其更加清晰。 implements 关键字可用于 interface 。一般来说,implements 用于接口中来实现功能。

abstract 是一个隐藏了一些功能而只显示必要功能的类。 Extend 关键字可用于抽象 类。一般来说,extends 可以用来扩展抽象类的功能。

Interface is nothing but the skeleton structure of a class and it is not class. we can just implement that class and add some extra feature to make more clear. The implements keyword can be used for interface .In general way implement is used in interface to implementing the features.

abstract is a class which hides some of features and only just shows the necessary features. Extend keyword can be used for abstract class. In general ways extends can be used to extends the features of abstract class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文