Application of abstract classes and abstract methods in PHP
Abstract class Interface Polymorphism
Abstract class is a special class, interface is a special kind of abstract class
Declaring abstract classes and interfaces, and some of the required techniques
Abstract class:
What is an abstract method?
Definition: If a method in a class does not have a method body method is an abstract method (that is, a method does not use {} and the semicolon ends directly)
function test (); Abstract methods
function test () {//has method body, but method body is empty
}
If a method is an abstract method, it must be decorated with the abstract keyword
Why use abstract methods?
What is an abstract class?
1. If one of the methods in a class is abstract, then this class is an abstract class.
2. If a class is an abstract class, the class must use the abstract adornment
3. Abstract class is a special class, because there are abstract methods in one class, others are unchanged
4. An abstract class cannot instantiate an object (an abstract class object cannot be created by an abstract class)
Role:
To use an abstract class, you must use a class to inherit the abstract class, and to use this subclass, that is, to have the subclass create the object, the subclass must no longer be an abstract class, and subclasses can override the method of the parent class (adding the method body to the abstract method)
Methods in the abstract method have no method body, subclasses must implement this method (the parent class does not write the concrete implementation, but the subclass must have this method name)
is to define some specifications so that subclasses implement these functions according to these specifications
Purpose: To add your own program module to the original written program
<?PHP//Define a demo classAbstract classdemo{var $name;//declaring member properties Abstract functionTest ();//defines an abstract method (no method body), which is modified with the keyword abstract functionSay () {//declaring member Methods Echo $thi-name; }}classJcdemoextendsdemo{//As with the parent's test, it overrides the abstract method of the parent class. functionTest () {Echo"Nihao!<br>"; }}//create an object for a class$J=NewJcdemo;//calling methods in a class$J-test ();?>
Interface:
Because PHP is single-inheritance, if you use an abstract class, subclasses can no longer inherit other classes after they implement the abstract class.
If you want to implement some specifications and you want to inherit a different class, you will use the interface
Comparison of interfaces and abstract classes
1. The same function, can not create objects, all need subclasses to implement
2. The declaration of an interface is not the same as the abstract class
3. The interface is implemented in a different way, using a class to implement the interface, using the word implements
4. All methods in an interface must be abstract methods, only abstract methods can be declared (without using the abstract adornment)
5. member properties of an interface, only constants can be declared, variables cannot be declared
6. The member access rights in the interface must be public, the lowest permission in the abstract class is protected
If the subclass is overriding an abstract method in the parent interface, use implements (Interface---> class interface----> abstract class)
Interface----> interface using extents
You can use abstract classes to implement some of the methods in an interface
If you want subclasses to create objects, you must implement all the methods in the interface;
You can define an interface to inherit another interface.
A class can implement multiple interfaces (to develop subclasses by multiple specifications), separating multiple interface names with commas
A class can implement one or more interfaces while inheriting a class (inherit first, then implement)
Two purposes for using implements
1. Multiple interfaces can be implemented, whereas extends words can only inherit one parent class
2. Not using extends words, you can go to inherit a class, two can be used together
declaring classes
Class Name {
}
Declaring abstract classes
Abstract class class Name {
}
declaring interfaces
Interface interface Name {
}
<?PHPInterfacedemo{//member properties of an interface, only constants can be declared, variables cannot be declared Consthost= "localhost"; ConstUser= "YY"; //all methods in an interface must be abstract methods, only abstract methods can be declared (without using the abstract adornment) functionfun1 (); //the member access permission in the interface must be public, the lowest permission in the abstract class is protected//protected function fun2 ();}//if the subclass is overriding an abstract method in the parent interface, use implements (Interface---> class interface----> abstract class)InterfaceDemo2extendsdemo{functionfun2 (); functionfun3 ();}Interfacedemo3{functionFun4 (); functionfun5 ();}classhello{functionfun6 () {}}//interfaces are implemented in a different way, using a class to implement an interface, using the word implements//can use abstract classes to implement some of the methods in the interface//If you want subclasses to create objects, you must implement all the methods in the interface;//interface----> Interfaces Use extents//a class can inherit one class at the same time, to implement one or more interfaces (inherit first, then implement)classTestextendsHelloImplementsdemo2,demo3{//A class can implement multiple interfaces functionfun1 () {}functionfun2 () {}functionfun3 () {}functionFun4 () {}functionfun5 () {}}EchoDemo::HOST;?>
Abstract classes and interfaces in PHP