PHP object clone keywords and _ clone () steps

Source: Internet
Author: User
PHP object clone keywords and the _ clone () method clone keywords are used to clone an identical object, __clone () method to override the original attributes and methods. When cloning an object, we need to use two or more identical objects in a project. if the new keyword is used to re-create the object, assign the same attribute to the object, this method is cumbersome and the clone keyword of PHP object and the _ clone () method
The clone keyword is used to clone an identical object. the __clone () method overwrites the original attributes and methods.

Object cloning

Sometimes we need to use two or more identical objects in a project. if we use the new keyword to re-create an object and assign values to the same attributes, this is cumbersome and error-prone. PHP provides the object cloning function. you can clone an identical object based on an object. after cloning, the two objects do not interfere with each other.

Use the keyword clone to clone objects. Syntax:
$object2 = clone $object;


Example:
 Name = $ name; $ this-> age = $ age;} function say () {echo "My name is:". $ this-> name ."
"; Echo" My age is :". $ this-> age ;}$ p1 = new Person ("zhang san", 20); $ p2 = clone $ p1; $ p2-> say ();?>


Running example, output:
My name is Zhang San. my age is: 20.


_ Clone ()

To change the content of the original object after cloning, you must add a special _ clone () method to the class to override the original attributes and methods. The _ clone () method is automatically called only when the object is cloned.

Example:
 Name = $ name; $ this-> age = $ age;} function say () {echo "My name is :". $ this-> name; echo "My age is :". $ this-> age."
";} Function _ clone () {$ this-> name =" I am a fake ". $ this-> name; $ this-> age = 30 ;}$ p1 = new Person ("James", 20); $ p1-> say (); $ p2 = clone $ p1; $ p2-> say ();?>


Running example, output:
My name is Zhang San. my age is 20. my name is: I am a fake man. my age is: 30.
1 floor meihaoderizi123 2012-08-14 thanks, this article is very helpful to me.

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.