Section 5 clone [5] _ PHP Tutorial

Source: Internet
Author: User
Section 5 clone [5]. The object model in PHP5 calls an object through reference, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, the object model in PHP 5 calls an object through reference, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone. like _ construct and _ destruct, the front has two underscores.

By default, the _ clone method is used to create an object with the same attributes and methods as the original object. if you want to change the default content during cloning, overwrite (attribute or method) in _ clone ).

The clone method can have no parameters, but it also contains the this and that pointers (that points to the copied object ). if you choose to clone yourself, you must be careful to copy any information contained in your object, from that to this. if you use _ clone to copy. PHP will not perform any implicit replication,

The following shows an example of automatic objects using sequence numbers: Class ObjectTracker file: // object tracker
{
Private static $ nextSerial = 0;
Private $ id;
Private $ name;

Function _ construct ($ name) file: // Constructor
{
$ This-> name = $ name;
$ This-> id = + self: $ nextSerial;
}

Function _ clone () file: // clone
{
$ This-> name = "Clone of $ that-> name ";
$ This-> id = + self: $ nextSerial;
}

Function getId () file: // Get the id attribute value
{
Return ($ this-> id );
}

Function getName () file: // get the value of the name attribute
{
Return ($ this-> name );
}
}

$ Ot = new ObjectTracker ("Zeev's Object ");
$ Ot2 = $ ot->__ clone ();

// Output: 1 Zeev's Object
Print ($ ot-> getId (). "". $ ot-> getName ()."
");

// Output: 2 Clone of Zeev's Object
Print ($ ot2-> getId (). "". $ ot2-> getName ()."
");
?>

But sometimes you may want to create a copy of the object, and expect that the change of the original object will not affect the copy. for this purpose, PHP decides...

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.