In PHP, when is an abstract class used and an interface used?

Source: Internet
Author: User
Please explain in detail why the abstract class is used instead of the interface, or why the interface is used instead of the abstract class
  • Please explain in detail why the abstract class is used instead of the interface, or why the interface is used instead of the abstract class

Reply content:
  • Please explain in detail why the abstract class is used instead of the interface, or why the interface is used instead of the abstract class

Emphasize actions with interfaces and classes with abstraction.

I have also thought about this problem. The two are indeed similar.

To use interfaces, you want to specify that developers (including themselves) need to implement a series of specific class methods.

When using abstract classes, you want to stipulate that developers (including themselves) need to implement a series of specific class methods and provide some basic class methods for developers to use.

Another difference is that a class can implement multiple interfaces, but can inherit only one abstract class. If your code is to standardize a very fine function (for exampleIteratorAnd so on. For others, you can use interfaces or abstract classes.

Finally, it is recommended that if you do not know whether to use an interface or an abstract class, you can define an interface and then write an abstract class that implements that interface.

An interface is a protocol, and an abstract class is equivalent to a class template.
Use abstract classes instead of interfaces to separate agreements and implementations.
If you need to provide the value type of the multi-State hierarchy, use the interface.
If one type must implement multiple protocols, or the Protocol applies to multiple types, use interfaces.
Although abstract classes and interfaces support separation of protocols and implementations, the interface cannot specify new members in later versions. abstract classes can add members as needed to support more functions.
Define classes rather than interfaces.

Fact abstract classes and interface classes do not affect the running of the program in the application. They only serve as a standard.

For example, abstract classes: for example, the current class can only be inherited and used, and cannot be directly referenced.

Interface Class: the biggest role is to play a normative role. After the underlying framework is compiled. For subsequent expansion, but the code specification must be ensured, the interface class is generally written so that developers can develop code in the future!

According to the characteristics, if only the standard interface is formulated, only constants and public abstract methods can be defined.

Abstract classes only mean that they cannot be instantiated. They can be written in the same way as common classes. Of course, they can also be used to write abstract methods.

Abstraction is used for different things, and interfaces are used for things.

In fact, the difference between an abstract class and an interface is not too big. An interface cannot have its own implementation. An abstract class can have its own implementation, both interfaces and abstractions are used to implement dependency abstraction. Do not rely on specific implementation principles ----**

Principle of dependency inversion

**

I think the interfaces in php are purely nonsense.

An interface is a key factor in lineage when multiple interfaces are inherited. An interface can define a business scenario .. For example, if you are a person inherited from an abstract class, you need to implement interfaces such as employees of a company and programmers. This method class has fewer levels. A company employee can also be the heir to an abstract class, and then the programmer inherits the employee of a company. See how to design. Trait and interfaces provide great reusability in the first Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.