1. Image extraction class (abstract)
In our actual development process, some classes do not need to be instantiated. For example, some of the parent classes we learned earlier mainly involve subclass inheritance, which improves code reusability.
Syntax structure:
Copy codeThe Code is as follows: abstract class name {
Attribute $ name;
Method () {}// method can also be the abstract modifier function Method Name (){}
}
Example:Copy codeThe Code is as follows: abstract class animal {
Public $ name;
Public $ age;
// The abstract method cannot have a method body, mainly to implement the subclass;
Abstract public function cry ();
// The abstract class can contain abstract methods and instance class methods.
Public function getname (){
Echo $ this-> name;
}
}
Class Cat {
Public function cry (){
Echo 'OK ';
}
}
Understanding: animals are actually an abstract concept that specifies the common attributes and behaviors of some animals, but in fact they themselves do not possess those attributes and behaviors. For example, transportation tools and plants
Note:
1. If a class is modified with abstract, the class is an abstract class. If a method is modified with abstract, the method is an abstract method, abstract METHODS cannot have method bodies => abstract function cry (); even {} cannot have
2. abstract classes cannot be instantiated. abstract classes can have no abstract methods. However, if a class contains any abstract method, this class must be declared as abstract;
3. If a class inherits another abstract class, the subclass must implement all abstract methods in the abstract class (unless it is declared as an abstract class itself );
2. Interfaces)
An interface encapsulates unimplemented methods into a class and writes them according to the actual situation;
Syntax structure
Interface name {
// Attributes and Methods
// Methods in the interface cannot have a method body;
}
How to implement interfaces
Class name implements interface name {
}
Understanding: an interface is a more abstract class. Methods in an abstract class can have a method body, but methods in the interface must have no method body. The interface implements the design idea of program design with polymorphism, high cohesion, and low coupling;
Example:Copy codeThe Code is as follows: // The interface defines specifications and attributes, generally starting with lower-case I;
Interface iUsb {
Public function start ();
Public function stop ();
}
// Compile the camera class to implement the interface
// When a class implements an interface, the class must implement all methods of the interface
Class Camera implements iUsb {
Public function start (){
Echo 'camera Start work ';
}
Public function stop (){
Echo 'camera Stop work ';
}
}
// Write a mobile phone
Class Phone implements iUsb {
Public function start (){
Echo 'phone Satrt work ';
}
Public function stop (){
Echo 'phone Stop work ';
}
}
$ C = new Camera ();
$ C-> start ();
$ P = new Phone ();
$ P-> start ();
When to use the interface:
1. Set standards to be implemented by other programmers
2. When multiple classes at the same level need to implement a function, but the implementation methods are different;
Summary:
1. The interface cannot be instantiated, and all methods in the interface cannot have a subject;
2. A class can implement multiple interfaces, separated by commas (,). class demo implements if1, if2, if3 {}
3. The interface can have attributes, but must be constants. constants cannot have modifiers (the default is the public modifier)
For example: interface iUsb {
Const A = 90;
}
Echo iUsb:;
4. All methods in the interface must be public. The default value is public;
5. One interface cannot inherit other classes, but it can inherit other interfaces. One interface can inherit multiple other interfaces.
For example: interface Name extends if1, if2 {}
6. A class can implement other interfaces while inheriting the parent class
Example: class test extends testbase implements test1, test2 {}
Implementation interface VS inheritance class
The inheritance of php is a single inheritance, that is, a class can only inherit one parent class, which affects the extension of the subclass function. The implementation interface can be seen as a supplement to the inherited class. Inheritance is a hierarchical relationship, which is not flexible, while the implemented interface is a hierarchical relationship. The implemented interface can be extended to a function without breaking the inheritance relationship.
Iii. Final
1. if we want a class not to be inherited by other classes (for security reasons, etc ..), You can consider using final.
Syntax:
Final class {}
2. If we want a method not to be overwritten by the quilt class, we can consider using final for modification. The final modification method can still be inherited, because the inheritance right of the method depends on the modification of public.
For example:Copy codeThe Code is as follows: class {
Final public function getrate ($ salary ){
Return $ salary * 0.08;
}
}
Class B extens {
// The getrate method of the parent class uses final, so the getrate cannot be rewritten here.
// Public function getrate ($ salary ){
// Return $ salary x 0.01;
//}
}
3. final cannot be used to modify attributes.
Iv. const)
In some cases, you may want to change the value of a member variable without changing it, in this case, you can use a const constant (the constant name should be in uppercase without the $ symbol, and the constant cannot be decorated)
Syntax:
Const constant name = constant value; // The initial value must be assigned, because the constant cannot be modified.
Call:
Class Name: constant name [internal use of this class self: constant name] Or Interface Name: constant name // The interface can only use constants, not Variables
For example:Copy codeThe Code is as follows: class {
Const TAX_RATE = 0.08;
Function paytax ($ salary ){
Return $ salary * self: TAX_RATE;
}
}
$ A = new ();
Echo $ a-& gt; paytax (100 );
Note:
1. constants can be inherited by quilt classes.
2. constants belong to a class instead of an object.