Php Object-oriented Abstract method and abstract class _ call clone object detailed tutorial

Source: Internet
Author: User
This article introduces the object-oriented abstract method and the usage of the abstract class _ call clone object in php. if you need to solve this problem, you can learn it. In OOP, a class can have one or more sub-classes, and each class has at least one public method as the external generation.

This article introduces the object-oriented abstract method and the usage of the abstract class _ call clone object in php. if you need to solve this problem, you can learn it.

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 instance code is as follows:

  1. Abstract function fun1 ();
  2. 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, the class must be declared as an abstract class and modified using abstract. for example:

The instance code is as follows:

  1. Abstract class Demo
  2. {
  3. Var $ test;
  4. Abstract function fun1 ();
  5. Abstract function fun2 ();
  6. Function fun3 ()
  7. {
  8. ......
  9. }
  10. }

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 instance code is as follows:

  1. Abstract class Demo
  2. {
  3. Var $ test;
  4. Abstract function fun1 ();
  5. Abstract function fun2 ();
  6. Function fun3 ()
  7. {
  8. ......
  9. }
  10. }
  11. // 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 instance code is as follows:

  1. $ Demo = new Demo ();
  2. Class Test extends Demo
  3. {
  4. Function fun1 ()
  5. {
  6. ......
  7. }
  8. Function fun2 ()
  9. {
  10. ......
  11. }
  12. }
  13. // The subclass can instantiate the object because all the abstract methods in the parent class are implemented.

The instance code is as follows:

  1. $ 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 will exit and cannot continue execution. 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 instance code is as follows:

  1. // This is a test class with no attributes or methods in it
  2. Class Test
  3. {
  4. }
  5. // Generate a Test class object
  6. $ Test = new Test ();
  7. // Call a method that does not exist in the object
  8. $ Test-> demo ("one", "two", "three ");
  9. // The program will not be executed here
  10. Echo "this is a test ";
  11. In the preceding example, the following error occurs, and the program cannot be executed after exit;
  12. 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 instance code is as follows:

  1. // This is a test class with no attributes or methods in it
  2. Class Test
  3. {
  4. // 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.
  5. Function _ call ($ function_name, $ args)
  6. {
  7. Print "the function you call: $ function_name (parameter :";
  8. Print_r ($ args );
  9. Print ") does not exist! N ";
  10. }
  11. }
  12. // Generate a Test class object
  13. $ Test = new Test ();
  14. // Call a method that does not exist in the object
  15. $ Test-> demo ("one", "two", "three ");
  16. // The program will not exit and can be executed here
  17. 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 instance code is as follows:

  1. Class Person
  2. {
  3. // The following are the member attributes of a person.
  4. Var $ name; // name of a person
  5. Var $ sex; // gender of a person
  6. Var $ age; // age of a person
  7. // Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
  8. Function _ construct ($ name = "", $ sex = "", $ age = "")
  9. {
  10. $ This-> name = $ name;
  11. $ This-> sex = $ sex;
  12. $ This-> age = $ age;
  13. }
  14. // This person can talk about his/her attributes.
  15. Function say ()
  16. {
  17. Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "";
  18. }
  19. }
  20. $ P1 = new Person ("zhang san", "male", 20 );
  21. // Use "clone" to clone the new object p2, which has the same attributes and methods as the p1 object.
  22. $ P2 = clone $ p1;
  23. $ 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 instance code is as follows:

  1. Class Person
  2. {
  3. // The following are the member attributes of a person.
  4. Var $ name; // name of a person
  5. Var $ sex; // gender of a person
  6. Var $ age; // age of a person
  7. // Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
  8. Function _ construct ($ name = "", $ sex = "", $ age = "")
  9. {
  10. $ This-> name = $ name;
  11. $ This-> sex = $ sex;
  12. $ This-> age = $ age;
  13. }
  14. // This person can talk about his/her attributes.
  15. Function say ()
  16. {
  17. Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "";
  18. }
  19. // 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 ().
  20. Function _ clone ()
  21. {
  22. // $ This refers to the copy p2, and $ that points to the original p1. in this way, the attributes of the copy are changed.
  23. $ This-> name = "I am a fake $ that-> name ";
  24. $ This-> age = 30;
  25. }
  26. }
  27. $ P1 = new Person ("zhang san", "male", 20 );
  28. $ P2 = clone $ p1;
  29. $ P1-> say ();
  30. $ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.