Section Fifth-Cloning--classesandobjectsinphp55_php tutorial

Source: Internet
Author: User
/* +-------------------------------------------------------------------------------+ | = This is haohappy read < > | = Notes in Classes and objects Chapter | = Translation-based + personal experience | = Please do not reprint in order to avoid the unnecessary trouble that may occur, thank you | = Welcome criticism, hope and all the PHP enthusiasts to progress together! +-------------------------------------------------------------------------------+/fifth-the object model in clone PHP5 invokes an object by reference. But sometimes you might want to create a copy of an object and want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone. Like __construct and __destruct, there are two underscores ahead. By default, the __clone method will establish an object that has the same properties and methods as the original object. If you want to change the default content when cloning, you need to overwrite (attribute or method) in __clone. A cloned method can have no arguments, but it contains both this and that pointer (that is, pointing to the object being copied). If you choose to clone yourself, you should be careful to copy any information you want your object to contain, from that to this. If you use __clone to copy. PHP does not perform any implicit replication, and the following shows an example of automating objects with series ordinals: name = $name; $this->id = ++self:: $nextSerial; } function __clone ()//clone {$this->name = "Clone of $that->name"; $this->id = ++self:: $nextSerial;} function get ID ()//Gets the value of the id attribute {return ($this->id),} function GetName ()//Gets the value of the Name property {return ($this->name);}} $ot = new Objecttracker ("Zeevs Object"); $ot 2 = $ot->__clone (); Output: 1 zeevs Object print ($ot->getid (). " " . $ot->getname (). "

"); Output: 2 Clone of Zeevs Object print ($ot 2->getid (). " " . $ot 2->getname (). "
");?>

http://www.bkjia.com/PHPjc/532190.html www.bkjia.com true http://www.bkjia.com/PHPjc/532190.html techarticle / * +-------------------------------------------------------------------------------+ | = This document is Haohappy read > | = Notes in Classes and objects Chapter | = Translation-based + personal experience ...

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