Php object-oriented full Guide (11) _ toString () usage clone object _ call handle call errors

Source: Internet
Author: User

16. _ toString () method
As we mentioned earlier, the methods for declaring the method names starting with "-" in the class (provided by PHP) are all in
The "_ toString ()" method is also automatically called at different times and under different circumstances.
It is automatically called when the object is referenced directly. We have mentioned that the object reference is a pointer, for example, "$ p = new
In Person () ", $ p is a reference. We cannot use echo to directly output $ p, which will output" Catchable fatal
Error: Object of class Person cocould not be converted to string ". If you define
The "_ toString ()" method automatically calls
The "_ toString ()" method outputs the characters returned by the "_ toString ()" method. Therefore, the "_ toString ()" method must be
A return statement is required ).
Code snippet Copy codeThe Code is as follows: <? Php
// Declare a simple class
Class TestClass {
Public $ foo;
Public function _ construct ($ foo ){
$ This-> foo = $ foo;
}
// Define a _ toString method and add a member attribute $ foo.
Public function _ toString (){
Return $ this-> foo;
}
}
$ Class = new TestClass ('hello ');
// Directly output the object
Echo $ class;
?>

Output in the previous example: Hello
17. Clone object
Sometimes we need to use two or more identical objects in a project. If you use "new"
If you re-create an object with a keyword, assign the same attribute to it, which is cumbersome and error-prone.
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;
Code snippetCopy codeThe Code is as follows: <?
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. "<br> ";
}
}
$ 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,
Use the "_ clone ()" method to create an object with the same attributes and methods as the original object.
The content of the original object must be rewritten in _ clone (). The "_ clone ()" method can have no parameters,
It automatically contains two pointers: $ this and $ that, $ this points to the duplicate, and $ that points to the original;
Code snippetCopy codeThe Code is as follows: 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. "<br> ";
}
// Method automatically called during object cloning. To change the content of the original object after cloning, rewrite the original content in _ clone ().
Attributes and methods
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:
Execution result
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.
18. _ call handle call errors
In program development, if the called method does not exist when an object calls an internal method of the object
An error occurs when the program exits. Can I call a method that does not exist inside the object in a program?
Prompt that the method we call and the parameters used do not exist, but the program can continue to execute, at this time we need
Use the "_ call ()" method that is automatically called when a non-existing method is called ()".
Code snippetCopy codeThe Code is as follows: <? Php
// 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 <br> ";
?>

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 the method that does not exist in the call.
When the _ call () method is automatically called, pass the method name of the nonexistent Method to the first parameter and the second parameter.
The number is to pass in multiple parameters of this method in the form of an array.
Code snippetCopy codeThe Code is as follows: <? Php
// 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! <Br> \ 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 <br> ";
?>

The output result of the preceding example is:
Execution result
The function you called: demo (parameter: Array ([0] => one [1] => two [2] => three) does not exist!
This is a test.

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.