Home > Backend Development > PHP Tutorial > Section 5--Clone_PHP Tutorial

Section 5--Clone_PHP Tutorial

WBOY
Release: 2016-07-21 16:00:58
Original
812 people have browsed it

/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy<>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/

Section 5--Clone

Object model in PHP5 by reference to call the object, 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 is the same, preceded by two underscores.

By default, using the __clone method will create an object with the same properties and methods as the original object. If you want to change the default content when cloning, you have to Override (property or method) 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 have to be careful 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.

The following shows an automated object using serial ordinals Example:

Copy code The code is as follows:
class ObjectTracker //Object Tracker
{
private static $nextSerial = 0;
private $id;
private $name;

function __construct($name) //Constructor
                                          $this->name = $name;
$this->id = ++self::$nextSerial;
} 🎜> $this->name = "Clone of $that->name";
$this->id = ++self::$nextSerial;
} }

function getId () //Get the value of the id attribute                                                       { Return ($ this-& gt; name);
}
}

$ OT = New Objecttracker ("Zeev's Object");
$ OT2 = $ OT-& GT ;__clone();

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316953.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ |=This article is Haohappy's notes from the chapter ClassesandObjects when reading CorePHP Programming |=Translation is mainly + personal...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template