The difference between PHP abstract classes and interfaces

Source: Internet
Author: User

The difference between PHP abstract classes and interfaces

Tags: abstract class Interface abstract class and interface PHP

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   AbstractClass {//enforces that subclasses are required to define these methods and cannot define specific functions note no 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 (such as $b in function eat ($a, $b =1) , which is an optional parameter), and no declaration of the parent abstract method is in 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
abstractclassextends Human{    abstractprivatefunction 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
abstractclass  Human{    abstractfunction eat();}abstractclassextends Human{    abstractfunction study();    //abstract function eat(); 若重写抽象父类的抽象方法eat()会报错}

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  {      constLEVEL=10;      publicfunction PlayLOL();      publicfunction PlayFootball();  
2. Implementation of the 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.
 InterfacePlay {Const  Level=Ten;       Public functionPlaylol();       Public functionPlayfootball();}InterfaceRead { Public functionReadnovel();}Abstract classhuman{Abstract functionEat();}//Abstract class 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, or you will get an errorAbstract classSutdentextendsHumanImplementsPlay,read{Abstract functionStudy();}
3. 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  {       publicfunction PlayFootball();  }interface Play1  {       publicfunction PlayFootball();  }interfaceextends play,Play1{      publicfunction 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.

The difference between PHP abstract classes and interfaces

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.