The difference between PHP abstract class and interface

Source: Internet
Author: User

First, look at what is abstract class and interface

 Abstract class:

A class that is defined as abstract cannot be instantiated. A class must be declared abstract if at least one of its methods is declared abstract. A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation. When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same (or looser) as the parent class. For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, and cannot be defined as private. The method must be called in the same way, i.e. the type and the required number of parameters must be the same. For example, a subclass defines an optional parameter, and the declaration of the parent abstract method does not, and the declaration does not conflict. This also applies to the PHP5.4 constructor. The constructor declaration before PHP5.4 can be different.

Interface:

Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.

Interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty.

All methods defined in the interface must be public, which is the attribute of the interface.

To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.

When implementing multiple interfaces, the methods in the interface cannot have duplicate names.

Interfaces can also be inherited by using the extends operator.

Class to implement an interface, it must be in a way that is exactly the same as the method defined in the interface. Failure to do so can result in fatal errors.

Difference:

1. The inheritance of interfaces uses implements, and abstract classes use extends.

2. You cannot declare a variable in an interface, but you can declare a class constant. Various variables can be declared in an abstract class

3. Interfaces do not have constructors, abstract classes can have

4. The methods in the interface are public by default, and methods in the abstract class can be decorated with public,protected,private

5. A class can inherit multiple interfaces, but can inherit only one abstract class

The difference between PHP abstract class and interface

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.