The usage of PHP Abstract Class and interface

Source: Internet
Author: User

When writing a program, I often get tangled up, whether an abstract object should be defined as an abstract class or an interface (Interface)? The two are very similar, even can be replaced with each other, difficult to choose. This question has been asked many times on StackOverflow, in all programming languages. In the comments under the Abstract Class and Interface chapters of the PHP official website, people are also arguing. In order to understand this problem, we must carefully compare the difference between the two and use the scene.

The structure of Abstract Class and Interface

Abstract class

<?phpabstract class A {    abstract public Function method1 ();    Abstract public Function method2 ();    Public Function method3 () {        //...    }}? >

Interface Interface

<?phpinterface B {public    function method4 ();    Public function Method5 ();}? >

As you can see, abstract classes can have an abstraction function (METHOD1,METHOD2), or a specific function implementation (METHOD3), whereas Interface can only define functions and not be implemented (METHOD4,METHOD5). Obviously, if the functions in an abstract class are all abstract functions, then this abstract class is degenerate into an interface. But it's important to note that

    • PHP and Java, a class can inherit only one Abstract class, but can implement multiple Interface. This is the so-called single inheritance system, that is, subcategories can inherit only one parent category, and one parent category can be inherited by multiple subcategories. It is said that multiple inheritance is possible in Python.
    • Attribute member variables (attribute) can be declared in the Abstract Class, and Interface cannot.
Ii. examples

The following example can help us to analyze the essential differences between the two, from another level: the design concepts reflected by the Abstract Class and Interface.

Suppose we want to define a class with a "gate" that has "open" and "off" two actions. At this point we can also use the abstract Class or Interface to describe this abstraction-

Abstract class

<?phpabstract class Door {    abstract public function open ();    Abstract public Function close ();}? >

Interface Interface

<?phpinterface Door {public    function open ();    Public function Close ();}? >

In the Abstract Class does not realize the door opening and closing method, because some doors can be opened with the key, some doors with a password open, and some doors can be opened with fingerprints, these specific methods to the sub-class inheritance to achieve. There is not much difference between the use of the Abstract Class and the Interface.

Now if you define a door with an "alarm" function, the Abstract Class and Interface describe a similar-

Abstract class

<?phpabstract class Door {    abstract public function open ();    Abstract public Function close ();    Abstract public Function alarm ();} Class Alarmdoor extends Door {public    function open () {        //...     }    Public function Close () {        //...     }    Public Function Alarm () {        //...     }}? >

Interface Interface

<?phpinterface door{public    function open ();    Public function Close ();    Public function Alerm ();} Class Alarmdoor implements Door {public    function open () {        //...     }    Public function Close () {        //...     }    Public Function Alarm () {        //...     }}? >

It is obvious that the above approach is wrong. It mixes the behavior method of opening, closing and other concept "alarm" in the definition of Door. Some doors can call the police, and some doors may not have the alarm function, but the alarm can be a variety of doors (key open, swipe the door, etc.). We have to define the alarm behavior separately into another object, then there are 3 ways:

    • The doors and alarms are defined in abstract class mode;
    • The doors and alarms are defined by the interface method;
    • One is defined using the abstract class method, and the other is defined using the interface method.

Since the PHP neutron class can only inherit from an abstract class, and the abstract class does not support multiple inheritance (abstract class cannot extend abstract class), the first way is obviously not. The second way is not able to correctly reveal our design intent, that is, does not reflect the alarmdoor in the concept is essentially Door, while it has an alarm function. So for the concept of Door, we should take the Abstract Class method to define, Alarmdoor inherit from Door, and the alarm concept is defined by Interface way.

<?phpabstract class Door {    abstract function open ();    Abstract function Close ();}interface Alarm {    function Alarm ();} Class Alarmdoor extends Door implements Alarm {public    function open () {        //...     }    Public function Close () {        //...     }    Public Function Alarm () {        //...     }}? >

Such an implementation basically correctly reflects our design intent. Interfaces can be multiple-inherited, and subclasses can implement multiple interfaces. This allows us to continue to expand the function of the door, such as the door to install a camera ...

Iii. Conclusion

When we use Abstract class, we should define the properties of a class of objects, that is, what it is . Interface is like a plug-in that focuses on providing additional capabilities and agreeing what it can do .

Reference Links:
Should I use an Abstract Class or a Interface?
Deep understanding of abstract class and interface

The usage of 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.