In the OOP language, a class can have one or more subclasses, and each class has at least one public method that accesses its interface as an external code. and abstract methods are introduced for the convenience of inheritance, let us first look at the definition of abstract and abstract methods to illustrate its purpose.
What is an abstract method? The method we define in our class is an abstract method , and the so-called no method body refers to the absence of curly braces and the contents of the method declaration, but rather the end of the method name with a semicolon at the time of declaration, plus a keyword when declaring an abstract method. abstract"to modify ;
For example:
Abstract function fun1 (); abstract function fun2 ();
The above example is the abstract method that "abstract" modifies without the method body "fun1 ()" and "fun2 ()", do not forget that there is a semicolon behind the abstract method; What is an abstract class? as long as there is one method in a class that is abstract, the class is defined as an abstract class , and the abstract class is also decorated with the "abstract" keyword, and in abstract classes there can be no abstract methods and member properties, but as long as one method is an abstract method, This class must be declared as an abstract class, decorated with "abstract".
For example:
Abstract class Demo{var $test; abstract function fun1 (); abstract function fun2 (); function fun3 () {...}}
In the example above, an abstract class "Demo" is defined using "abstract" to define a member attribute "$test", and two abstract methods "Fun1" and "fun2", and a non-abstract method fun3 (); So how do we use abstract classes? The most important point is that the abstract class can not produce an instance object , so it can not be used directly, we mentioned many times before the class can not be used directly, we use the object through the class instantiation, then the abstract class can not produce an instance object we declare the abstract class what is the use of it? We use the abstract method as a template for subclass overloading, and defining an abstract class is equivalent to defining a specification that requires subclasses to obey, and after subclasses inherit abstract classes, the abstract methods inside the abstract class are implemented according to the needs of the subclasses. subclasses must implement all the abstract methods in the parent class, or else there are abstract methods in the subclass, then the subclass is abstract or the class cannot be instantiated ; Why do we have to inherit from the abstract class? Because sometimes we have to implement some functions must inherit from the abstract class, otherwise these functions you can not realize, if inherit the abstract class, it is necessary to implement the abstract method of the class;
<?abstract class Demo{var $test, abstract function fun1 (), abstract function fun2 (), function fun3 () {...}} $demo = new Demo (); An abstract class cannot produce an instance object, so it is wrong to instantiate the object to the subclass class Test extends Demo{function fun1 () {...} function fun2 () {...}} $test = new test (); Subclasses can instantiate an object because all abstract methods in the parent class are implemented?>
"Getting Started with PHP object-oriented (OOP) programming" 19. Abstract methods and abstract classes