The difference between abstract classes and interfaces in PHP

Source: Internet
Author: User

The difference between PHP abstract classes and interfaces

Introduction: This is an interview often asked questions, but also a classic question. We try to use the official authority's instructions or experiment to prove the accuracy of the content described herein.

Abstract class

Official description Please review the documentation, below is the official description of the grooming version:

    • A class that is defined as abstract cannot be instantiated. Any class, if at least one of its methods is declared abstract, then the class must be declared abstract. (Abstract classes can have no abstract methods, but abstract classes still cannot be instantiated) are defined as abstract methods that simply declare their invocation methods (parameters) and cannot define their specific function implementations. Such as

Abstract class  AbstractClass {    //requires subclasses to define these methods, and cannot define specific functions note without curly braces {}     abstract protected function  GetValue ();    Abstract protected function  Prefixvalue ($prefix);     Common method (non-abstract method) public     function  PrintOut () {        print  $this-GetValue ().  " \ n ";    }}
    • When inheriting an abstract class, the non-abstract subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same as (or looser) in 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 (similar to the function eat (

      $b in B=1) is an optional parameter , and the declaration of the parent abstract method does not, and the declaration does not conflict. This also applies to constructors from PHP 5.4. The constructor declaration before PHP 5.4 can be different.

    • Add:

      1. Abstract classes can have member properties.

      2. Some people ask: whether an abstract method can be defined as private, the answer is no, because the purpose of the abstract interface is to abstract the class model to inherit, defined as private, external access is not, offset the design purposes. The following will be an error

Abstract class Sutdent extends human{    abstract Private Function study ();}

Fatal error:abstract function Sutdent::study () cannot be declared private in D:\11\index.php on line 10

    1. Abstract classes can inherit abstract classes and cannot override abstract methods of the abstract parent class. This usage can be understood as an extension of the abstract class. Such as

Abstract class  human{    abstract function Eat ();} Abstract class Sutdent extends human{    abstract function Study ();    Abstract function eat (); If the abstract method of overriding the abstract parent class eat () will be an error}

The following error is reported if the abstract method of overriding the abstract parent class

Fatal Error:can ' t inherit abstract function Human::eat () (previously declared abstract in sutdent) in D:\11\index.php on Line 11

Interface

1. Definition of the 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 interface's characteristics, and protected and private will error (Fatal error:access type for interfacemethod).

    • Constants: Constants can also be defined in an interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or sub-interfaces. (It is not recommended to use this, it is not surprising what the meaning, but also easy to produce and abstract class confusion)

Interface Play  {      const level=10;      Public function playlol ();      Public function Playfootball ();  }

Implementation of # # # interface

To implement an interface, use the implements operator. All methods defined in an interface must be implemented in a non-abstract class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.
Add:
can inherit abstract class and implement interface at the same time, extends to write in front, abstract class implements interface, do not need to re-method.

  • 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.

Interface Play  {      const level=10;      Public function playlol ();      Public function Playfootball ();  } Interface Read  {public      function readnovel ()  } abstract class  human{    abstract function Eat ();} Abstract classes can implement the interface after the implementation of its methods, you can inherit an abstract class while implementing multiple interfaces note that you must write the extends statement in front of implements, otherwise it will be an error abstract class Sutdent extends Human Implements play,read{    abstract function study ();}

Inheritance of interfaces

Interfaces can inherit one or more interfaces, using the extends keyword, multiple separated by ', ', but not implementing another interface, and of course not inheriting abstract classes (Inheritance abstract class Error:Fatal Error:playgame cannot implement Human-itis not a interface in D:\11\index.php on line ten)

Interface Play  {public       function playfootball ();  } Interface Play1  {public       function playfootball ();  } Interface PlayGame extends play,play1{public      function Playlol ();  

Summarize

Generally are here to write the same point and different points, I do not write, hey, because I think the above written enough detail.

We summarize briefly: Abstract classes are typically used to define what a class of entities is, and he contains attributes, abstract methods, and non-abstract methods. Interfaces are used to define what a class of entities can do , and it is generally assumed that he has only abstract methods, and that constants are seldom used.

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.