The characteristics, differences and choices of abstract classes and interfaces in PHP

Source: Internet
Author: User

A. Features:

1, abstract class characteristics

(1) Using abstract to modify a class, then this class is an abstract class, abstract class can never be instantiated, that is, $ABC = new abstract class name (); (2) using abstract to modify a method, then the method is an abstract method, (3) If there is an abstract method in the class, then the class must be defined as an abstract class;but in turn, abstract classes do not necessarily have abstract methods. In addition, abstract classes can also have common methods. (4) Abstract methods cannot have method bodies. That is, abstract function abc ();------cannot be appended with parentheses {...}. (5), a class inherits an abstract class, it must implement all the abstract methods in the abstract class (unless, it is also these abstract methods are declared abstract, equivalent to the abstract class inherits the abstract class).

2, the characteristics of the interface

(1) The method of the interface must be public.
(2) The method of the interface is abstract by default, so it is not preceded by the method name.
(3) An interface can define constants, but cannot define member properties, and constants are defined and used the same as constants in a class.
(4) class can implement multiple interfaces (separated by commas)
(5) Interfaces can also inherit interfaces.

Two. Difference:

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. You cannot declare member variables (including class static variables) in an interface, but you can declare class constants. Abstract classes can declare various types of member variables to implement the encapsulation of data.

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

4, the method in the interface is the public type by default, and the methods in the abstract class can be decorated with private,protected,public.

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

Three. Select:

    • If you are creating a model, this model will be used by some closely related objects, and you can use abstract classes. The interface is used if you want to create features that will be used by some unrelated objects.
    • If the behavior must be inherited from multiple sources, the interface is used.
    • If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it.

Four. Example:

Abstract class People{var $name; abstract function Sayname ();} Interface mother{function Beautiful ();} Interface Father{function Clever ();} Class son extends people implements mother,father{function beautiful () {echo "I ' m very beautiful!";} Function Clever () {echo "I ' m very clever!";} function __construct ($name) {$this->name = $name;} function Sayname () {echo $this->name;}} $zww = new Son ("Tom"), $zww->beautiful (); $zww->clever (); $zww->sayname ();

Output result: I ' m very beautiful!i ' m very clever! Tom

The characteristics, differences and choices of abstract classes and interfaces in PHP

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.