First, the extraction class (abstract)
In our actual development process, some classes do not need to be instantiated, such as some of the previous learning of the parent class, mainly for the subclass to inherit, so as to improve code reusability
Syntax structure:
The code is as follows |
Copy Code |
Abstract class class Name { attribute $name; Method () {}//method can also be the abstract modifier function method name () {} } |
Cases:
The code is as follows |
Copy Code |
abstract class animal{ public $name; public $age; //Abstract methods can not have method body, mainly to let subclasses to achieve; Abstract Public Function cry (); //Abstract classes can contain abstract methods, and can also contain instance class methods Public Function GetName () { echo $this->name; } } class cat{ public Function Cry () { echo ' OK '; } } |
Understanding: An animal is, in fact, an abstract concept that prescribes some of the common properties and behaviors of some animals, but in fact it itself and the confiscation of those attributes and behaviors. For example: vehicles, plants and so on
Note:
1, if a class is decorated with abstract, then the class is an abstract class, and if a method is modified by the abstract, Then the method is an abstract method, the abstract method cannot have the method body and abstract function cry (); even {} can not have
2, abstract class must not be instantiated, Abstract classes can have no abstract methods, but if a class contains any abstract method, the class must be declared as an abstract class;
3, if a class inherits another abstract class, the subclass must implement all abstract methods in the abstract class (unless it declares itself as an abstract class);
Second Interface (interface) The
interface is to encapsulate some of the methods that are not implemented, and then write them out according to specific circumstances when a class is used.
syntax structure
copy code |
tr>
Interface interface Name { // Properties, Methods // Methods in an interface cannot have a method body; } How to implement interfaces class Name implements interface name { |
Understanding: Interfaces are abstract classes that are more abstract, and methods in an abstract class can have a method body, but the methods in an interface must have no method body. The interface realizes the design idea of multi-state and high cohesion and low-coupling of programming.
Cases:
The code is as follows |
Copy Code |
An interface is a definition of a specification, a property, usually preceded by a lowercase i; Interface iusb{ Public function start (); Public function Stop (); } Write the camera class and let it implement the interface When a class implements an interface, the class must implement all the methods of the interface Class Camera implements iusb{ Public Function Start () { Echo ' Camera Start work '; } Public Function Stop () { Echo ' Camera Stop work '; } } Write a phone class 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 specifications, let other programmers to achieve
2, when a number of peer class, all need to implement a function, but the way of implementation is different;
Summary:
1, the interface can not be instantiated, the interface of all methods can not have the subject;
2, a class can implement multiple interfaces, separated by a comma (,) class demo implements if1,if2,if3{}
3, the interface can have attributes, but must be constants, constants can not have modifiers (default is the public modifier)
such as: interface iusb{
Const A=90;
}
Echo iusb::a;
4, the method in the interface must be public, the default is public;
5, an interface cannot inherit other classes, but can inherit other interfaces, an interface can inherit multiple other interfaces
Example: interface interface name extends if1,if2{}
6. A class can implement other interfaces while inheriting the parent class
such as: Class Test extends Testbase implements test1,test2{}
Implementing Interfaces vs Inheriting classes
The inheritance of PHP is a single inheritance, that is, a class can only inherit a parent class, so that the extension of the function of the subclass has a certain effect. Implementing an interface can be seen as a complement to an inherited class. Inheritance is a hierarchical relationship, not too flexible, and implementation of the interface is a level of relations, implementation of the interface can not break the inheritance relationship under the premise of a function extension, very flexible.
Third, Final
1. If we want a class to not be inherited by other classes (for security reasons, for example). ), you might consider using the final
Grammar:
Final Class a{}
2, if we want a method, not quilt rewrite, you can consider using final to decorate, the final modified method can be inherited, because the inheritance of the method depends on the decoration of the public
Such as:
The code is as follows |
Copy Code |
Class a{ Final public Function getrate ($salary) { return $salary *0.08; } } Class B Extens a{ The Getrate method of the parent class here uses final, so there is no way to rewrite the getrate Public Function Getrate ($salary) { return $salary *0.01; //} } |
3, final cannot be used to modify attributes
four. The class constant (const)
In some cases, there might be a requirement: When you do not want a member variable to be modified and you want the value of the variable to be fixed, you can use the const constant (the constant name should all be capitalized and without the $ symbol, Constants cannot be added modifiers)
Syntax:
Const constant NAME = constant value; //must be assigned an initial number, because constants are
calls that cannot be modified:
class Name:: Constant name [internal self:: constant name] or interface name:: Constant name // Only constants can be used in an interface, not variables
such as:
The code is as follows |
Copy Code |
Class a{ Const tax_rate=0.08; function Paytax ($salary) { return $salary *self::tax_rate; } } $a =new A (); echo $a->paytax (100); |
Note:
1, constants can be inherited quilt class
2. Constants belong to a class, not to an object
Although the implementation is very simple, as long as you have a little basic knowledge to facilitate the rapid implementation of PHP object-oriented editing operations.
PHP Object-Oriented extraction class, interface, final, class constants