The content abstraction interface final described in this section uses object-oriented constants in PHP to define classes to sample objects, the abstract class and interface class of PHP can be said to be a kind of specification of the class. by defining these two types, the class is forcibly bound, although both of them are class constraints, but they are actually different.
Content described in this section
Abstraction
Interface
Use of final
Class constant
Preface
In PHP, object-oriented objects are represented by defining classes. PHP's abstract classes and interface classes can be said to be a kind of class specification, define these two types to enforce the constraints on the class. although these two types are constraints on the class, they are essentially different.
Abstract class
Abstract class concept we can use the inheritance relationship of animals to illustrate the problem. when we write the parent class Animal class, there are two methods: sleep (), eat (), because you do not know the specific animal, you cannot determine what content is written in the method. This can be implemented using abstract classes.
When some methods of the parent class are uncertain, you can use the abstract keyword to modify the method. abstract classes are called abstract classes.
Basic syntax:
Abstract class name {abstract access modifier function name (parameter list );}During development, we can use abstract methods to override the methods of all subclasses that inherit the class.An abstract class is like a template. with a template, you can implement specific functions based on the template.The relationship between the template and the specific thing is passed through the inheritance class. Just like a computer, you can use a template to create a computer. Abstract classes are defined to be inherited.
After a class inherits the abstract class, all abstract methods of the parent class must be implemented in the subclass.The error reported above means that the parent class contains two abstract methods, and the subclass must implement this abstract method. Abstract classes can be summarized as follows:
Abstract classes Cannot be used as examples. you Cannot use abstract classes to create an object. a Cannot instantiate abstract class error is returned.
Abstract classes can have no abstract methods. they are all common methods in abstract classes, but class names are modified using abstract.
Abstract classes can have common Member methods and attributes.
If an abstract method exists in a class, the class must be an abstract class.
Abstract methods do not have a method body.
abstract public function eat();
No {}, that is, no method body.
If a class inherits the abstract class, the subclass must implement all abstract methods of the abstract class, or the subclass itself must be declared as an abstract class.
InterfaceThe original intention of the interface is the same as that of the abstract class. I don't know how to implement the interface in the method. The interface definition is: provide some unimplemented methods, encapsulate them together, and write these methods according to the actual situation when a class is to be used, the appearance of interfaces reflects the characteristics of high cohesion and low coupling.
Basic syntax:
Interface name {// method. in the interface, all methods cannot have method bodies, that is, abstract methods}The interface class is roughly the same as the abstract class. what is the specific interface? The abstract class mentioned above is like the shelf and template of a notebook, and then a specific notebook is created based on the template. no notebook has several usb interfaces, and the interface class is like the interfaces on these laptops, is an extension implementation. Just like animals, they all inherit the unique features of animals, such as eating and sleeping, but suddenly an animal has the ability to write somewhere else. this ability is expanded through interfaces.
The interface name generally starts with a lower-case I letter. InAll methods in the interface class are abstract methods by default.Therefore, you do not need to write abstract statements.
Interface implementationWe define an interface to implement other classes. here we talk about implementation, rather than inheritance, and the implementation relationship between interfaces and other classes, that is, the class implements an interface and is implemented using the implements keyword.
Interface iTechnical {public function fly ();} class Dog implements iTechnical {public function fly () {echo'
Fei ';}}Of course, all methods of the interface must be implemented in the subclass.
The interface features include the following:
Interface classes cannot be instantiated like abstract classes.
All methods in the interface cannot have subjects, because they are abstract methods.
One class can implement multiple interfaces separated by commas (,) (inheritance can only be one)
Class Dog implements Interface 1, Interface 2 {}
The interface can have attributes, but can only be a constant. the default value is public, but it cannot be explicitly modified by public.
All methods in the interface must be public, and the interface is used for inheritance, so public is used. If no modifier is written, the default value is public.
An interface cannot inherit other classes, but it can inherit other interfaces.
Differences between abstraction and interfacesIn PHP, inheritance is a single inheritance, that is, a class can only have one parent class at most. this single inheritance mechanism can ensure the purity of the class. However, this mechanism has a certain impact on the extension of subclass functions.
The emergence of interfaces can be said to be a supplement to inheritance. Inheritance is hierarchical and not flexible, but interfaces are not more flexible than abstraction. In addition, the sub-class function is expanded without breaking the inheritance relationship.
The relationship between them can be understood as follows:
Use of finalIn the above introduction, each class can be inherited. if we have a class, we do not want the subclass to override a method in it, or we do not want other classes to inherit the class, you can use the keyword final. Final: the final, final, can be used to modify classes or methods.
FinalThe method or class can be modified. if the method is modified, it indicates that the method cannot be overwritten by the inherited class. if final modifies a class, it indicates that the class cannot be inherited.
Basic syntax:
Final class name {} class name {final access modifier function name (shape parameter ){}}Modifier class and modifier method.
The class modified with final cannot be inherited.
It can be seen from the results that the final-defined method cannot be rewritten.
When using final, it can only be used to modify classes or methods, but not attributes. After a class is modified by final, the methods in the class do not need to be modified by final. because classes cannot be inherited, the methods cannot be rewritten. At the same time, the final modified class can be instantiated.
If a method is modified by final, it can be inherited but cannot be overwritten.
Sum (1, 2);... result... 3When we write the Singleton mode, we say that the singleton mode was not perfect at the time. we can get different objects through inheritance. here we use the final keyword to modify the singleton class, to prevent inheritance, you cannot inherit from other objects.
Class constantClass constants and common constants are a concept. if you do not want a member variable to be modified, you want the value of this variable to be fixed. In this case, you can use const to modify the member variable so that the variable automatically becomes a constant. Constants in a class are called class constants. As mentioned above, there are two ways to define constants: define () and the keyword const. in the class, you must use const. if you use define (), an error is returned.
GetPI (); echo'
'; Echo A: PI; ...... result ........ 3.1415926 3.1415926Class constants are generally named in uppercase letters, separated by underscores (_), and there is no $ symbol before a constant. A constant must be assigned a value when it is defined. At the same time, there cannot be modifiers before constants. the default value is public.
Constant accessThere are two types of constant access: one is access within the class, and the other is access outside the class.
Internal access
Access by class name: constant name.
Class A {const PI = 3.1415926; public function getPI () {return A: PI; // access by class name }}Access by self: constant name
Class A {const PI = 3.1415926; public function getPI () {return self: PI; // access by class name }}The access via self indicates that constants belong to classes and do not belong to objects.
External access
Access by class name: constant name.
echo A::PI;
Access by object name: constant name
$a = new A();echo $a::PI;
However, we recommend that you use the first class name for access.
If a class contains constants, constants can be inherited during inheritance. At the same time, the constant data type cannot be an object.
SummaryAbstract and interface applications in PHP allow us to better understand the skeleton of requirements and build our code system. At the same time, abstract and interface are used to reduce the coupling degree of code. Facilitate code extension.
The above is the abstract and interface content of PHP basic tutorial 12. For more information, see PHP Chinese Web (www.php1.cn )!