Differences between php abstract classes and interfaces,

Source: Internet
Author: User

Differences between php abstract classes and interfaces,

1. The use of the interface is through the keyword implements. The use of abstract classes is through the keyword extends. Of course, the interface can also be inherited by the keyword extends.

2. member variables (including static class variables) cannot be declared in the interface, but class constants can be declared. Abstract classes can declare various types of member variables to encapsulate data.


3. The interface has no constructor, and the abstract class can have constructor.

 

4. Methods in the interface are of the public type by default, while methods in the abstract class can be modified using private, protected, and public.

5. A class can implement multiple interfaces at the same time, but a class can only inherit from one abstract class.

 

Abstract class or interface.

  • If you want to create a model, this model will be used by closely related objects, you can use abstract classes. If you want to create functions that will be used by irrelevant objects, use interfaces.
    • If the behavior must be inherited from multiple sources, the interface is used.
    • If you know that all classes share a public behavior implementation, use an abstract class and implement this behavior in it.

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.