Abstract methods and abstract classes
In OOP, a class can have one or more sub-classes, and each class has at least one public method as an external code to access its interface. Abstract methods are introduced to facilitate Inheritance. Let's take a look at the definition of abstract classes and abstract methods and then describe their usage.
What is an abstract method? The method defined in the class without a method body is an abstract method. The so-called "no method body" means that there is no braces or content in the method declaration, instead, add a semicolon to the end of the method name during declaration, and add the keyword "abstract" to declare the abstract method. For example:
The code is as follows: |
Copy code |
Abstract function fun1 (); Abstract function fun2 (); |
In the above example, there is an abstract method "fun1 ()" and "fun2 ()" modified by abstract without a method body. Do not forget that there is a semicolon after the abstract method; so what is an abstract class? As long as a method in a class is an abstract method, this class should be defined as an abstract class, and the abstract class should also be modified using the "abstract" keyword; abstract classes can contain non-abstract methods and member attributes. However, if a method is an abstract method, this class must be declared as an abstract class and modified using "abstract. For example:
The code is as follows: |
Copy code |
Abstract class Demo { Var $ test; Abstract function fun1 (); Abstract function fun2 (); Function fun3 () { ...... } } |
In the above example, an abstract class "Demo" is defined and "abstract" is used for modification. In this class, a member attribute "$ test" is defined ", and two abstract methods "fun1" and "fun2" have a non-abstract method fun3 (). How can we use abstract classes? The most important thing is that abstract classes cannot generate instance objects, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. We use objects instantiated by classes, so abstract classes cannot generate instance objects. What is the purpose of declaring abstract classes? Abstract methods are used as templates for subclass overloading. Defining an abstract class is equivalent to defining a standard that requires sub-classes to comply with. After the sub-classes follow the abstract class, implement the abstract methods in the abstract class according to the requirements of the subclass. The subclass must implement all the abstract methods in the parent class. Otherwise, there are still abstract methods in the subclass, so the subclass or abstract class still cannot be instantiated. Why do we have to inherit from the abstract class? Sometimes, to implement some functions, we must inherit from the abstract class; otherwise, you cannot implement these functions. If the abstract class is inherited, we must implement the abstract methods in the class;
The code is as follows: |
Copy code |
Abstract class Demo { Var $ test; Abstract function fun1 (); Abstract function fun2 (); Function fun3 () { ...... } } |
// The abstract class is an instance object that can be generated. Therefore, this is incorrect. The instantiated object is handed over to the subclass.
The code is as follows: |
Copy code |
$ Demo = new Demo (); Class Test extends Demo { Function fun1 () { ...... } Function fun2 () { ...... } } |
// The subclass can instantiate the object because all the abstract methods in the parent class are implemented.
The code is as follows: |
Copy code |
$ Test = new Test (); |
_ Call handle call errors
In program development, if the internal method of the object is called when the object does not exist, the program will fail and the program cannot continue to execute after exiting. Then, when the program calls a method that does not exist inside the object, it prompts that the method we call and the parameters we use do not exist, but the program can continue to execute, in this case, we need to use the "_ call ()" method that is automatically called when a non-existing method is called ()".
The code is as follows: |
Copy code |
// This is a test class with no attributes or methods in it Class Test { } // Generate a Test class object $ Test = new Test (); // Call a method that does not exist in the object $ Test-> demo ("one", "two", "three "); // The program will not be executed here Echo "this is a test "; |
In the preceding example, the following error occurs, and the program cannot be executed after exit;
Fatal error: Call to undefined method Test: demo ()
Next, we add the "_ call ()" method. This method has two parameters. The first parameter is used to automatically call the _ call () method when a nonexistent method is called, pass the method name of this nonexistent method to the first parameter, and the second parameter is to pass multiple parameters of this method in the form of an array.
The code is as follows: |
Copy code |
// This is a test class with no attributes or methods in it Class Test { // The method automatically called when a non-stored method is called. The first parameter is the method name, and the second parameter is the array parameter. Function _ call ($ function_name, $ args) { Print "the function you call: $ function_name (parameter :"; Print_r ($ args ); Print ") does not exist! N "; } } // Generate a Test class object $ Test = new Test (); // Call a method that does not exist in the object $ Test-> demo ("one", "two", "three "); // The program will not exit and can be executed here Echo "this is a test "; |
The output result of the preceding example is:
The function you called: demo (parameter: Array ([0] => one [1] => two [2] => three) does not exist! This is a test.
Clone object
Sometimes we need to use two or more identical objects in a project. If you re-create an object using the "new" keyword, assign the same attribute to it, this method is cumbersome and error-prone. Therefore, it is necessary to clone an identical object based on an object. After cloning, the two objects do not interfere with each other.
In PHP5, we use the "clone" keyword to clone the object;
The code is as follows: |
Copy code |
Class Person { // The following are the member attributes of a person. Var $ name; // name of a person Var $ sex; // gender of a person Var $ age; // age of a person // Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age. Function _ construct ($ name = "", $ sex = "", $ age = "") { $ This-> name = $ name; $ This-> sex = $ sex; $ This-> age = $ age; } // This person can talk about his/her attributes. Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. ""; } } $ P1 = new Person ("zhang san", "male", 20 ); // Use "clone" to clone the new object p2, which has the same attributes and methods as the p1 object. $ P2 = clone $ p1; $ P2-> say (); |
PHP5 defines a special method named "_ clone ()", which is automatically called during object cloning and uses "_ clone () the method creates an object with the same attributes and methods as the original object. To change the content of the original object after cloning () override the original attributes and methods. The "_ clone ()" method can have no parameters. It automatically contains two pointers $ this and $ that, and $ this points to the duplicate, and $ that points to the original;
The code is as follows: |
Copy code |
Class Person { // The following are the member attributes of a person. Var $ name; // name of a person Var $ sex; // gender of a person Var $ age; // age of a person // Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age. Function _ construct ($ name = "", $ sex = "", $ age = "") { $ This-> name = $ name; $ This-> sex = $ sex; $ This-> age = $ age; } // This person can talk about his/her attributes. Function say () { Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. ""; } // Method automatically called during object cloning. If you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in _ clone (). Function _ clone () { // $ This refers to the copy p2, and $ that points to the original p1, so that the attributes of the copy are changed in this method. $ This-> name = "I am a fake $ that-> name "; $ This-> age = 30; } } $ P1 = new Person ("zhang san", "male", 20 ); $ P2 = clone $ p1; $ P1-> say (); $ P2-> say (); Output in the preceding example: |
My name is John. Gender: Male. My age is: 20.
My name is: I am a fake gender of Michael Jacob. My male age is: 30.