Php interface and abstract class usage example

Source: Internet
Author: User
This article mainly introduces the php interface and abstract class examples, and explains in detail. if you need them, you can refer to section 1. abstract class

1. abstract class refers to a class with abstract keywords added before the class and abstract methods (abstract keywords added before the function keyword of the class method.

2. abstract classes cannot be directly instantiated. Abstract classes only define (or partially implement) the methods required by subclass. Subclass can realize the concrete type of an abstract class by inheriting the abstract class and implementing all abstract methods in the abstract class.

3. if the subclass needs to be instantiated, the premise is that it implements all abstract methods in the abstract class. If the subclass does not fully implement all abstract methods in the abstract class, the subclass is also an abstract class. the abstract keyword must be added before the class and cannot be instantiated.

The code is as follows:
Abstract class
{
/** Variables can be defined in the abstract class */
Protected $ value1 = 0;
Private $ value2 = 1;
Public $ value3 = 2;
/** You can also define non-abstract methods */
Public function my_print ()
{
Echo "hello, world/n ";
}
/**
* In most cases, an abstract class contains at least one abstract method. Abstract methods are declared with the abstract keyword, which cannot contain specific content.
* An abstract method can be declared as a general class method, but should end with a semicolon instead of a method body. That is to say, abstract methods cannot be implemented in abstract classes, that is, there is no function body "{some codes }".
*/
Abstract protected function abstract_func1 ();
Abstract protected function abstract_func2 ();
}
Abstract class B extends
{
Public function extends act_func1 ()
{
Echo "implement the role act_func1 in class A/n ";
}
/** An error will be reported when this is written in zend studio 8 */
// Abstract protected function abstract_func2 ();
}
Class C extends B
{
Public function extends act_func2 ()
{
Echo "implement the role act_func2 in class A/n ";
}
}

4. if A subclass B inherited from A is created as follows, but the abstract method abstract_func () is not implemented ():

The code is as follows:
Class B extends {};

The program will encounter the following errors:

The code is as follows:
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_func () method, the access control of the abstract_func () method in B cannot be stricter than that of the abstract_func () method in A, that is:

(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 as protected, then the statement of abstract_func () in B can be public or protected, but not private.

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

II. interface

1. abstract classes provide specific implementation standards, while interfaces are pure templates. The interface only defines the function and does not contain the implemented content. The interface is declared with the keyword interface.

2. interfaces are completely abstract. they can only declare methods. they can only declare public methods, private and protected methods, method bodies, and instance variables. However, the interface can declare constant variables. However, putting constant variables in an interface violates its purpose as an interface and obfuscated the different values of interfaces and classes. If necessary, you can put it in the corresponding abstract class or Class.

The code is as follows:
Interface iA
{
Const AVAR = 3;
Public function iAfunc1 ();
Public function iAfunc2 ();
}
Echo iA: AVAR;

3. all classes that implement interfaces must implement all methods defined in interfaces.

The code is as follows:
Class E implements iA
{
Public function iAfunc1 () {echo "in iAfunc1 ";}
Public function iAfunc2 () {echo "in iAfunc2 ";}
}

Otherwise, the class must be declared as abstract.

The code is as follows:
Abstract class E implements iA {}

4. a class can use the implements keyword in the declaration to implement an interface. After doing so, the specific process of implementing the interface is the same as inheriting an abstract class containing only abstract methods. A class can inherit a parent class and implement any number of interfaces at the same time. The extends clause should be before the implements clause. PHP only supports inheriting from one parent class, so only one class name can be followed after the extends keyword.

The code is as follows:
Interface iB
{
Public function iBfunc1 ();
Public function iBfunc2 ();
}
Class D extends A implements iA, iB
{
Public function extends act_func1 ()
{
Echo "implement the role act_func1 in class A/n ";
}
Public function extends act_func2 ()
{
Echo "implement the role act_func2 in class A/n ";
}
Public function iAfunc1 () {echo "in iAfunc1 ";}
Public function iAfunc2 () {echo "in iAfunc2 ";}
Public function iBfunc1 () {echo "in iBfunc1 ";}
Public function iBfunc2 () {echo "in iBfunc2 ";}
}

Class D extends B implements iA, iB
{
Public function extends act_func1 ()
{
Parent: abstract_func1 ();
Echo "override the abstract_func1 in class A/n ";
}
Public function extends act_func2 ()
{
Echo "implement the role act_func2 in class A/n ";
}
Public function iAfunc1 () {echo "in iAfunc1 ";}
Public function iAfunc2 () {echo "in iAfunc2 ";}
Public function iBfunc1 () {echo "in iBfunc1 ";}
Public function iBfunc2 () {echo "in iBfunc2 ";}
}

5. the interface cannot implement another interface, but it can inherit multiple

The code is as follows:
Interface iC extends iA, iB {}
Class F implements iC
{
Public function iAfunc1 () {echo "in iAfunc1 ";}
Public function iAfunc2 () {echo "in iAfunc2 ";}
Public function iBfunc1 () {echo "in iBfunc1 ";}
Public function iBfunc2 () {echo "in iBfunc2 ";}
}

III. similarities and differences between abstract classes and interfaces

1. similarities:

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

(2) Both the interface implementation class and the abstract class subclass must implement the declared abstract method.

2. differences:

(1) interfaces must be implemented and implements must be used, while abstract class must be inherited and extends must be used.

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

(3) interfaces emphasize the implementation of specific functions, while abstract class emphasizes the relationship.

(4) although both the interface implementation class and the abstract class subclass must implement the corresponding abstract method, the implementation form is different. Every method in the interface is an abstract method and is declared only (declaration, no method body). The implementation class must be implemented. Abstract class sub-classes can be selectively implemented. This choice has two meanings: a) not all methods in abstract class are abstract. only those methods with abstract titles are abstract, and subclass must be implemented. If there are no abstract methods, the method body must be defined in abstract class; B) when the subclass of abstract class inherits from it, non-abstract methods can be directly inherited or overwritten; for an abstract method, you can select implementation or leave it to its subclass for implementation, but this class must also be declared as an abstract class. It is an abstract class and cannot be instantiated.

(5) abstract class is the intermediary between interface and class. Abstract class plays an important role in interface and class. Abstract class is abstract and can declare abstract methods to standardize the functions that must be implemented by Subclass. on the other hand, abstract class can define the default method body for subclass to use or override directly. In addition, it can also define its own instance variables for subclass to use through inheritance.

(6) abstract keywords cannot be added before abstract methods in the interface. by default, abstract methods are implicitly used, and final keywords cannot be added to prevent inheritance of abstract methods. Abstract indicates that the declaration is an abstract method.

(7) abstract methods in the interface are public by default, and can only be public. private and protected modifiers cannot be used for modification. Abstract methods in abstract classes can be modified using public and protected, but private.

3. application scenarios of interfaces

(1) coordination between classes requires specific interfaces, regardless of how they are implemented.

(2) as an identifier that can implement a specific function, it can also be a pure identifier that does not exist in any interface method.

(3) a group of classes must be considered as a single class, and the caller only associates with this group of classes through interfaces.

(4) a specific number of functions need to be implemented, and these functions may be completely unrelated.

4. application scenarios of abstract class

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

(1) defines a group of interfaces, but does not want to force each implementation class to implement all interfaces. Abstract class can be used to define a set of method bodies, or even empty method bodies, and then the subclass will select the methods that interest it to override.

(2) in some cases, the pure interface alone cannot satisfy the coordination between classes, and the variables in the class that indicate the state must be used to distinguish different relations. Abstract's mediation can well meet this requirement.

(3) standardizes a set of methods for mutual coordination, some of which are common and independent of the state. they can be shared without sub-classes; in other ways, each subclass needs to implement its own specific functions based on its specific state.

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.