You can use abstract to decorate a class or a method.
The class represented by an abstract class means that the class is an abstraction, and the method represented by an abstract modification is a method of abstraction.
Abstract classes cannot be instantiated.
Abstract methods are only method declarations, and there is no way to implement content.
Abstract Abstraction Class
You can use an abstract to decorate a class.
A class that is decorated with an abstract indicates that the class is an abstract class.
Abstract classes cannot be instantiated.
This is a simple abstract method, and if it is instantiated directly, the system will make an error.
PHP code
The following example normaluser inherits from the user class and can be instantiated.
PHP code
It is meaningless to set an abstract class individually, and only with abstract methods can the abstract class have flesh and blood. The abstract method is described below.
Abstract Abstraction Method
It is an abstract method to represent this method with an abstract-modified class.
Abstract method, only the declaration part of the method, there is no method body.
Abstract method does not have {}, but adopts; End.
In a class, as long as there is an abstract method, the class must be declared as an abstract class.
An abstract method must be overridden in a subclass.
The following is an abstract class with two abstract methods, Setsal () and Getsal () respectively. Used to retrieve $sal employees ' wages.
PHP code
Since the user class cannot be inherited directly, we write a Normaluser class that inherits from the user class. When we write the following code, the system will error. This error tells us that there are two abstract methods in the user class, and we must override both methods in the subclass.
PHP code
In the following example, the two methods are rewritten, although the contents of the {} inside the method body are empty, this method is also overridden. Note The parameter names of the overridden methods, where the number of arguments is the same, and the names of the parameters must be identical.
PHP code
The following 19-21 lines, three ways to write overrides will be error.
19 rows, missing parameters.
20 lines, more parameters.
21 rows, the parameter type is not correct. (This is described in later chapters)
In a class, if there is an abstract method, the class must be declared as an abstract class.
The following class is not an abstract class, where an abstract method is defined and an error is made.
PHP code
Abstract class inherits abstract class
When an abstract class inherits another abstract class, it does not have to override the abstract method.
Abstract methods of the abstract parent class cannot be overridden in an abstract class.
This usage can be understood as an extension of the abstract class
The following example shows that an abstract class inherits from another abstract class and does not need to override the abstract method.
PHP code
Abstract classes cannot be overridden after they are inherited by an abstract method.
If a rewrite occurs, the system will error.
PHP code
Abstract classes inherit abstract classes for the purpose of extending the abstract class.
PHP code
In PHP5.1, the static abstract method is supported in the abstract class. In the following example, we see that the static abstract method can be declared. When implementing this method, it must be a static method.
Static abstract methods
In PHP5.1, the static abstract method is supported in the abstract class. In the following example, we see that the static abstract method can be declared. When implementing this method, it must be a static method.
PHP code
There seems to be no problem with the abstract method here