php 何时实施和扩展?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4961906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:22:47  来源:igfitidea点击:

When to implement and extend?

phpoopextend

提问by Industrial

  • When should implementor extendbe used?
  • What are some real-world examples?
  • 什么时候应该implementextend什么时候使用?
  • 有哪些现实世界的例子?

Is this correct?

这样对吗?

Implementingappears to be a way to enforce that certain methods exists in a class, and that these methods function calls are properly formatted. Implementing is nota way of passing variables or "settings" to the class though?

实现似乎是一种强制某些方法存在于类中的方法,并且这些方法函数调用的格式正确。实现不是将变量或“设置”传递给类的一种方式?

Expected real-life scenario:I have an e-commerce platform featuring multiple payment classes that all follow the same design. When a new payment class should be added, it's really easy to follow the defined design of the interfaceto ensure that all bits and pieces are there, from the beginning.

预期的现实生活场景:我有一个电子商务平台,其中包含多个均遵循相同设计的支付类别。当应该添加一个新的支付类时interface,从一开始就很容易遵循定义的设计以确保所有的点点滴滴都在那里。

Extendingclasses makes the extended (child?) class inherit everything from its parent class except methods & variables declared as private?

扩展类使扩展(子?)类继承其父类的所有内容,除了声明为private?

Expected real-life scenario:I have one class called sessionswith two child classes named sessioncookiesand databasesessions. sessioncookiesand databasesessions, together inherit a number of mutual config options from their parent sessions, making it easy to change a config option to affect all sorts of eventual visitor data storage.

预期的现实生活场景:我有一个类sessions,其中有两个名为sessioncookies和 的子类databasesessionssessioncookiesdatabasesessions,一起从其父会话继承了许多相互配置选项,从而可以轻松更改配置选项以影响各种最终访问者数据存储。

采纳答案by Rafe Kettler

Inheritance is useful to reduce the amount of code you rewrite. If you have several classes with a few common methods or fields, instead of defining these methods and fields over and over you can factor them into a base class and have each of the child classes extend that base class.

继承对于减少重写的代码量很有用。如果您有几个具有一些通用方法或字段的类,您可以将它们分解为一个基类,并让每个子类扩展该基类,而不是一遍又一遍地定义这些方法和字段。

Interfaces (and implements) are useful when you'd like to define a common protocolfor how a group of objects should behave. For example, you might want to mandate that objects that are comparable can be compared for equality and hashed, etc.

implements当您想为一组对象的行为方式定义通用协议时,接口(和)很有用。例如,您可能希望强制可以比较可比较的对象的相等性和散列等。

Using inheritance is ultimately a design choice. Be on the lookout for cases where you define the same methods across several classes; those are excellent cases where you can factor those methods out into a base class. The same goes for classes that observe some of the same characteristics: you can guarantee consistency by putting those characteristics in an interface to be implemented by those related classes.

使用继承最终是一种设计选择。注意在多个类中定义相同方法的情况;在这些情况下,您可以将这些方法分解为基类。对于观察到一些相同特征的类也是如此:您可以通过将这些特征放在由这些相关类实现的接口中来保证一致性。

Inheritance is a big concept in OOP that goes way beyond just PHP. I recommend you read the wikipedia article on inheritanceand perhaps Design Patterns by the Gang of Four.

继承是 OOP 中的一个重要概念,它不仅限于 PHP。我建议你阅读维基百科关于继承的文章,也许还有四人帮的设计模式

I believe your understanding of inheritance is mainly correct. The next step would be to use it in production.

我相信你对继承的理解主要是正确的。下一步是在生产中使用它。

回答by Ahmad Mobaraki

Implement:

实施:

Interfaces are abstract classes, so you can only declare things. A class implements an interface. You can implement multipleinterfaces.

接口是抽象类,因此您只能声明事物。一个类实现一个接口。您可以实现多个接口。

Extend:

延长:

You extendclasses when you want a more specific versionof a class.

当您需要更具体的类版本时,您可以扩展类。

Example:

例子:

// Contract: a pet should play
public interface Pet {
    public void play(); 
}

// An animal eats and sleeps
class Animal {
    public void eat();
    public void sleep();
}


public class Dog extends Animal implements Pet {

    public void play() {...}
}


public class Camel extends Animal {
}

Both Camel and Dog are animals, so they extend Animalclass. But only the Dog is a specific kindof Animalthat alsocan be a Pet

Camel 和 Dog 都是动物,因此它们扩展了Animal类。但只有狗是一种特殊Animal可以是Pet