Section fifth-cloning-Classes and Objects in PHP5 [5]

Source: Internet
Author: User
Tags object model
Section Fifth-cloning
The object model in PHP5 invokes an object by reference, but sometimes you might want to create a copy of an object and expect 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 in front.
By default, the __clone method creates an object that has the same properties and methods as the original object. If you want to change the default content during cloning, you will overwrite (attribute or method) in the __clone.
A cloned method can have no parameters, but it also contains this and that pointer (that points to the object being copied). If you choose to clone yourself, you have to be careful to copy any information that you want your object to contain, from that to this. If you use __clone to copy. PHP does not perform any recessive replication,
An example of automating an object with a series ordinal is shown below:
Green C Hinaip ower. com7at0x
<?php
Class Objecttracker//Object Tracker
{
private static $nextSerial = 0;
Private $id;
Private $name;
function __construct ($name)//constructor
{
$this->name = $name;
$this->id = ++self:: $nextSerial;
}
function __clone ()//Clone
{
$this->name = "Clone of $that->name";
$this->id = ++self:: $nextSerial;
}
function getId ()//Get the value of the id attribute
{
Return ($this->id);
}

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.