Home  >  Article  >  Backend Development  >  Section 5 - Cloning - Classes and Objects in PHP5 [5]_PHP Tutorial

Section 5 - Cloning - Classes and Objects in PHP5 [5]_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:05:11739browse

Section 5 - Cloning
The object model in PHP5 calls objects by reference, but sometimes you may want to create a copy of the object, and hope that changes to the original object will not affect the copy. For this purpose, PHP defines A special method called __clone. Like __construct and __destruct, there are two underscores in front.
By default, using the __clone method will create an object with the same properties and methods as the original object. If If you want to change the default content when cloning, you have to override (properties or methods) in __clone.
The clone method can have no parameters, but it contains both this and that pointers (that points to the copied object) . If you choose to clone yourself, you need to be careful to copy any information you want your object to contain, from that to this. If you use __clone to copy. PHP will not perform any implicit copying,
shown below An example of using serial numbers to automate objects:
Green c hinaip ower.com7AT0x
class ObjectTracker //Object tracker
{
private static $nextSerial = 0;
private $id;
private $name;
function __construct($name) //Constructor function
$this->name = $name; >id = ++self::$nextSerial;
                                                                                                                                                                                                        ; ;
         $this->id = ++self::$nextSerial; ;id);
        }





http://www.bkjia.com/PHPjc/445143.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/445143.html

TechArticleSection 5 - Cloning the object model in PHP5 calls objects by reference, but sometimes you may want to build a A copy of the object, and hope that changes to the original object will not affect the copy. In order to do this...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn