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

Source: Internet
Author: User
As we mentioned earlier, the methods for declaring the method name starting with "-" in the class (provided by PHP) are automatically called and executed at different times. _ 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

 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 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 snippet

 Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // The way this person can speak, say your own property function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}}$ P1 = new Person (" James "," male ", 20); // use" clone "to clone the new object p2, it 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 snippet

Class Person {// The following is the member attribute var $ name of a Person; // The name of a Person var $ sex; // gender var $ age of a Person; // person's age // 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;} // The way this person can talk, say his own attribute function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";} // Method automatically called when the object is cloned. to change the content of the original object after cloning, you need () rewrite the original attribute and method function _ clone () {// $ this indicates the copy p2, and $ that indicates the original p1, so that in this method, changed the duplicate attributes. $ 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 snippet

 Demo ("one", "two", "three"); // The program will not run 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 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 snippet

 \ 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:
Execution result
The function you called: demo (parameter: Array ([0] => one [1] => two [2] => three) does not exist!
This is a test.

For more information about object-oriented php solutions _ toString () usage, clone object _ call, and handle call errors. For more information, see The PHP Chinese website!

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: 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.