Abstract class and interface (interface) in PHP

Source: Internet
Author: User
Tags what interface
Abstract class

1. An abstract class is a class that has an abstract keyword in front of the class and an abstraction method (which adds the abstract keyword before the class method function keyword).

2. Abstract classes cannot be instantiated directly. Only the methods required for defining (or partially implementing) subclasses in an abstract class. Subclasses can make abstract classes materialized by inheriting abstract classes and by implementing all the abstract methods in the abstract class.

3. If a subclass needs to be instantiated, it implements all the abstract methods in the abstract class. If the subclass does not fully implement all the abstract methods in the abstract class, then the subclass is an abstract class that must precede the class with the abstract keyword and cannot be instantiated.

[C-sharp] View plaincopyprint?

  1. Abstract class A
  2. {
  3. /** You can define variables in abstract classes * /
  4. protected $value 1 = 0;
  5. private $value 2 = 1;
  6. public $value 3 = 2;
  7. /** can also define non-abstract methods * /
  8. Public Function My_print ()
  9. {
  10. echo "hello,world/n";
  11. }
  12. /**
  13. * In most cases, abstract classes contain at least one abstract method. Abstract methods are declared with the abstract keyword, which cannot have specific content.
  14. * You can declare an abstract method as you would declare a normal class method, but end with a semicolon instead of a method body. This means that abstract methods cannot be implemented in abstract classes, that is, there is no function body "{Some codes}".
  15. */
  16. Abstract protected function abstract_func1 ();
  17. Abstract protected function Abstract_func2 ();
  18. }
  19. Abstract class B extends A
  20. {
  21. Public Function abstract_func1 ()
  22. {
  23. echo "Implement the Abstract_func1 in class a/n";
  24. }
  25. /** wrote this in Zend Studio 8.
  26. //abstract protected function Abstract_func2 ();
  27. }
  28. class C extends B
  29. {
  30. Public Function Abstract_func2 ()
  31. {
  32. echo "Implement the Abstract_func2 in class a/n";
  33. }
  34. }


4. If you create a subclass B that inherits from a as follows, but does not implement the abstract Method Abstract_func ():

[PHP] View plaincopyprint?

    1. Class B extends a{};

Then the program will appear with the following error:

[PHP] View plaincopyprint?

    1. Fatal error:class B contains 1 abstract Method and must therefore be declared abstract or implement the remaining met Hods (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 more restrictive than the access control of Abstract_func () in A, which means:

(1) if Abstract_func () in A is declared 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 as protected, then the declaration of Abstract_func () in B can be public or protected, but not private

(3) if Abstract_func () in a is declared private, hehe, can not be defined as private Oh! (Fatal error:abstract function A::abstract_func () cannot be declared private)

Second, interface interface

1. Abstract classes provide a concrete implementation of the standard, and the interface is purely a template. The interface defines only the functionality, not the content of the implementation. The interface is declared with the keyword interface.

2. Interface are completely abstract, can only declare methods, and can only declare methods of public, cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables . However, interface can declare constant variables . But putting constant variable in interface violates its purpose of being an interface, and it confuses the different value of interface and class. If you really need it, you can put it in the appropriate abstract class or class.

[PHP] View plaincopyprint?

    1. Interface IA
    2. {
    3. Const avar=3;
    4. Public function iAfunc1 ();
    5. Public function iAfunc2 ();
    6. }
    7. echo IA:: AVAR;

3. Any class that implements an interface implements all the methods defined in the interface

[PHP] View plaincopyprint?

    1. class E implements IA
    2. {
    3. Public function iAfunc1 () {echo "in IAfunc1";}
    4. Public function IAfunc2 () {echo "in IAfunc2";}
    5. }

Otherwise, the class must be declared abstract.

[PHP] View plaincopyprint?

    1. Abstract class E implements ia{}

4. A class can use the Implements keyword in a declaration to implement an interface. After doing so, the exact process of implementing the interface is the same as inheriting an abstract class that contains only abstract methods. A class can inherit a parent class and implement any number of interfaces at the same time. The extends clause should precede the implements clause. PHP only supports inheriting from a 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. class D extends B implements Ia,ib
  22. {
  23. Public function abstract_func1 ()
  24. {
  25. Parent::abstract_func1 ();
  26. Echo "Override the Abstract_func1 in class a/n";
  27. }
  28. Public function Abstract_func2 ()
  29. {
  30. Echo "Implement the Abstract_func2 in class a/n";
  31. }
  32. Public function iAfunc1 () {echo "in IAfunc1";}
  33. Public function IAfunc2 () {echo "in IAfunc2";}
  34. Public function iBfunc1 () {echo "in IBfunc1";}
  35. Public function IBfunc2 () {echo "in IBfunc2";}
  36. }


5. Interfaces can not implement another interface, but can inherit multiple

[PHP] View plaincopyprint?

    1. interface IC extends ia,ib{}
    2. class F implements IC
    3. {
    4. Public function iAfunc1 () {echo "in IAfunc1";}
    5. Public function IAfunc2 () {echo "in IAfunc2";}
    6. Public function iBfunc1 () {echo "in IBfunc1";}
    7. Public function IBfunc2 () {echo "in IBfunc2";}
    8. }


Iii. similarities and differences of abstract classes and interfaces

1. The same point:

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

(2) The subclass of interface implementation class and abstract class must implement an abstract method that has already been declared.

2. Different points:

(1) interface need to implement, to use implements, and abstract class need to inherit, to use extends.

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

(3) interface emphasizes the realization of a particular function, while the abstract class emphasizes its affiliation.

(4) Although the subclass of the interface implementation class and the abstract class must implement the corresponding abstract method, the implementation is different in form. Each method in interface is an abstract method, all just declared (declaration, no method body), the implementation class must be implemented. The subclass of abstract class can be implemented selectively. This option has a two point meaning: a) Not all methods in abstract class are abstract, only those whose crowns have abstractions are abstract and must be implemented by subclasses. Those that have no abstract method, must define the method body in the abstract class, B) The subclass of abstract class inherits it, the non-abstract method can be either directly inherited or overwritten, and the abstract method can be chosen to implement, but also left to its subclasses to implement, However, this class must also be declared as an abstract class. Abstract classes, and of course, cannot be instantiated.

(5) Abstract class is an 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 be used to declare abstractions to standardize the functions that subclasses must implement, on the other hand, it can define the default method body for the subclasses to use or overwrite directly. In addition, it can define its own instance variables for use by subclasses through inheritance.

(6) Before the abstract method in the interface can not add the abstract keyword, the default implicit is an abstract method, and can not add the final keyword to prevent the inheritance of abstract methods. Abstract methods in abstract classes must be preceded by an abstract representation to display the declaration as an abstraction.

(7) The abstract method in the interface is public by default, and can only be public, and cannot be decorated with the private, protected modifier. Abstract methods in abstract classes can be modified with public, protected, but not private.

3. Interface applications

(1) Classes and classes require a specific interface to reconcile, regardless of how they are implemented.

(2) as an identity that is capable of achieving a specific function, it can also be a purely identifier of what interface method does not exist.

(3) A set of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.

(4) A specific number of functions need to be implemented, and there may be no connection between these features at all.

4. Application of abstract class

In a word, it can be used in situations where both a uniform interface is required and an instance variable or a default method is required. The most common are:

(1) Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use the abstract class to define a set of method bodies, even an empty method body, and then select the methods that you are interested in to overwrite by subclasses.

(2) On some occasions, purely interfaces do not satisfy the coordination between classes and classes, and the variables that represent states in a class must differentiate between different relationships. Abstract mediation can be a good way to meet this point.

(3) standardize a set of mutually coordinated methods, some of which are common, state-independent, shareable, without subclasses, and others require individual subclasses to implement specific functions according to their specific state.

The above describes the abstract class in PHP and the interface (interface), including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

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