Home > Backend Development > PHP Tutorial > Abstract class and interface in PHP

Abstract class and interface in PHP

WBOY
Release: 2016-08-08 09:22:46
Original
1438 people have browsed it

1. Abstract class abstract class

1. An abstract class refers to a class with the abstract keyword added before class and an abstract method (the abstract keyword added before the class method function keyword).

2. Abstract classes cannot be instantiated directly. The abstract class only defines (or partially implements) the methods required by the subclass. Subclasses can make an abstract class concrete by inheriting it and by implementing all the abstract methods in the abstract class.

3. If a subclass needs to be instantiated, it must implement all abstract methods in the abstract class. If the subclass does not implement all abstract methods in the abstract class, then the subclass is also an abstract class and must be preceded by the abstract keyword in class and cannot be instantiated.

[c-sharp] view plaincopyprint?

  1. abstract class A
  2. {
  3. /** Variables can be defined in abstract classes*/
  4. protected $value1 = 0;
  5. private $value2 = 1;
  6. public $value3 = 2;
  7. */
  8. public function my_print()
  9. {
  10. echo
  11. "hello,world/n"; /**You can also define non-abstract methods*/
  12. abstract
  13. protected function abstract_func1();
  14. abstractprotected function abstract_func2();
  15. abstract
  16. class B extends A
  17. {
  18. public
  19. function abstract_func1() {
  20. echo "implement the abstract_func1 in class A/n"; }
  21. /**
  22. * In most cases, an abstract class contains at least one abstract method. Abstract methods are declared with the abstract keyword and cannot have specific content.声 像*can declare abstract methods like a general class method, but it must be ends by segmentation rather than the method body. That is to say, abstract methods cannot be implemented in abstract classes, that is, there is no function body "{some codes}".
  23. */
  24. //abstract protected function abstract_func2();
  25. }
  26. class C extends B
  27. {
  28. public function abstract_func2()
  29. { echo
  30. "implement the abstract_func2 in class A/n"
  31. ; 4. If you create a subclass B that inherits from A as follows, but does not implement the abstract method abstract_func():
  32. [php] view plaincopyprint?
  33. Class B extends
  34. A{};
  35. Then the program will have the following error:
  36. [php] view plaincopyprint?
    1. Fatal error: Class B contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (A::abstract_func)

    5. If B implements the abstract method abstract_func(), then the access control of the abstract_func() method in B cannot be stricter than the access control of abstract_func() in A, that is to say:

    (1) If abstract_func() in A is declared as public , then the declaration of abstract_func() in B can only be public , not protected or private

    (2) If abstract_func() in A is declared protected , then the declaration of abstract_func() in B can be public or protected , but It cannot be private

    (3) If abstract_func() in A is declared as private, hehe, it cannot be defined as private! (Fatal error: Abstract function A::abstract_func() cannot be declared private)

    2. Interface interface

    1. Abstract classes provide standards for concrete implementation, while interfaces are pure templates. Interfaces only define functions, not implementation content. Interfaces are declared with the keyword interface.

    2. Interface is completely abstract. It can only declare methods, and only public methods. Private and protected methods cannot be declared. Method bodies cannot be defined, and instance variables cannot be declared. However, interface can declare constant variables. But placing constant variables in interface violates the purpose of its existence as an interface, and also confuses the different values ​​​​of interface and classes. If you really need it, you can put it in the corresponding abstract class or Class in.

    [php] view plaincopyprint?

    1. interface iA
    2. {
    3. const AVAR=3;
    4. public function iAfunc1();
    5. public function iAfunc2();
    6. }
    7. echo
    8. iA::AVAR;
    3. Any class that implements an interface must implement all methods defined in the interface

    [php] view plaincopyprint?

    1. classpublic function iAfunc1(){echo
    2. "in iAfunc1"
    3. ;}
    4. public function iAfunc2(){echo "in iAfunc2";}
    5. }
    6. Otherwise The class must be declared abstract . [php] view plaincopyprint?
    abstract

    class

    A class can implement an interface using the implements keyword in its declaration. After doing this, the specific process of implementing the interface is the same as inheriting an abstract class that only contains abstract methods. A class can inherit a parent class and implement any number of interfaces at the same time. The extends clause should come before the implements clause. PHP only supports inheritance from one parent class, so the extends keyword can only be followed by a class name.

    [php] view plaincopyprint?
      1. interface iB  
      2. {  
      3.     public function iBfunc1();  
      4.     public function iBfunc2();  
      5. }  
      6. class D extends A implements iA,iB  
      7. {  
      8.     public function abstract_func1()  
      9.     {  
      10.        echo "implement the abstract_func1 in class A/n";  
      11.     }  
      12.     public function abstract_func2()  
      13.     {  
      14.        echo "implement the abstract_func2 in class A/n";  
      15.     }  
      16.     public function iAfunc1(){echo "in iAfunc1";}  
      17.     public function iAfunc2(){echo "in iAfunc2";}  
      18.     public function iBfunc1(){echo "in iBfunc1";}  
      19.     public function iBfunc2(){echo "in iBfunc2";}  
      20. }  
      21.    
      22. class D extends B implements iA,iB  
      23. {  
      24.     public function abstract_func1()  
      25.     {  
      26.        parent::abstract_func1();  
      27.        echo "override the abstract_func1 in class A/n";  
      28.     }  
      29.     public function abstract_func2()  
      30.     {  
      31.        echo "implement the abstract_func2 in class A/n";  
      32.     }  
      33.     public function iAfunc1(){echo "in iAfunc1";}  
      34.     public function iAfunc2(){echo "in iAfunc2";}  
      35.     public function iBfunc1(){echo "in iBfunc1";}  
      36.     public function iBfunc2(){echo "in iBfunc2";}  
      37. }  

      5 .接口不可以实现另一个接口,但可以继承多个

      [php] view plaincopyprint?

      1. interface iC extendsiA,iB{}
      2. classF implementsiC
      3. {
      4. public function iAfunc1(){echo " in iAfunc1";}
      5. public function iAfunc2(){echo "in iAfunc2";}
      6. public function iBfunc1(){echo "in iBfunc1" ;} public
      7. function iBfunc2(){echo "in iBfunc2";} }
      8. 3. Abstract classes and interfaces Similarities and Differences
      1. Similarities:

      (1) Both are abstract classes and cannot be instantiated.

      (2) Interface implementation classes and subclasses of abstract class must implement the declared abstract methods.

      2. Differences:

      (1) Interface needs to be implemented, using implements , while abstract class needs to be inherited, using extends .

      (2) A class can implement multiple interfaces, but a class can only inherit one abstract class.

      (3) Interface emphasizes the implementation of specific functions, while abstract class emphasizes the ownership relationship.

      (4) Although both interface implementation classes and abstract class subclasses must implement corresponding abstract methods, the implementation forms are different. Every method in interface is an abstract method, which is only declared (declaration, no method body), and the implementation class must implement it. Subclasses of abstract class can be implemented selectively. This choice has two implications: a) Not all methods in abstract class are abstract. Only those methods with abstract are abstract and must be implemented by subclasses. Those methods without abstract are in abstract The method body must be defined in class; b) When a subclass of abstract class inherits it, it can directly inherit or override non-abstract methods; for abstract methods, it can choose to implement it, or it can be left to its subclasses to implement , but this class must also be declared abstract. Since it is an abstract class, of course it cannot be instantiated.

      (5) Abstract class is the intermediary between interface and class. abstract class plays a connecting role in interface and class. On the one hand, abstract class is abstract and can declare abstract methods to standardize the functions that subclasses must implement; on the other hand, it can define default method bodies for direct use or override by subclasses. In addition, it can define its own instance variables for use by subclasses through inheritance.

      (6) The abstract keyword cannot be added before the abstract method in the interface. The abstract method is implicit by default, and the final keyword cannot be added to prevent the inheritance of abstract methods. In an abstract class, abstract must be added before the abstract method to indicate that it is explicitly declared as an abstract method.

      (7) Abstract methods in interfaces are public by default, and can only be public. They cannot be modified with private or protected modifiers. Abstract methods in abstract classes can be modified with public and protected, but cannot be modified with private.

      3. Application scenarios of interface

      (1) Classes need specific interfaces for coordination, regardless of how they are implemented.

      (2) It exists as an identifier that can implement a specific function, or it can be a pure identifier without any interface methods.

      (3) It is necessary to treat a group of classes as a single class, and the caller only contacts this group of classes through the interface.

      (4) It is necessary to implement multiple specific functions, and these functions may have no connection at all.

      4. Application occasions of abstract class

      In a word, you can use it when you need both a unified interface and instance variables or default methods. The most common ones are:

      (1) Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use abstract class to define a set of method bodies, or even empty method bodies, and then let subclasses choose the methods they are interested in to cover.

      (2) In some cases, pure interfaces alone cannot satisfy the coordination between classes. Variables representing states in the class are also required to distinguish different relationships. The intermediary role of abstract can satisfy this very well.

      (3) Standardizes a set of mutually coordinated methods, some of which are common, state-independent, and can be shared, without the need for subclasses to implement them separately; while other methods require each subclass to implement it according to its own specific state to achieve specific functions.

      The above introduces the abstract class (abstract class) and interface (interface) in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template