<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Object cloning</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="类与对象(PHP 5)" HREF="language.oop5.html"><LINK REL="PREVIOUS" TITLE="Final Keyword" HREF="language.oop5.final.html"><LINK REL="NEXT" TITLE="Comparing objects" HREF="language.oop5.object-comparison.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.oop5.final.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 19. 类与对象(PHP 5)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.oop5.object-comparison.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop5.cloning" >Object cloning</A ></H1 ><P > Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy. </P ><P > An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN6083" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >$copy_of_object = clone $object;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P > When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references. If a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6086" ></A ><P ><B >例 19-31. Cloning an object</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">SubObject<br /></font><font color="#007700">{<br /> static </font><font color="#0000BB">$instances </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /> </font><font color="#0000BB">public $instance</font><font color="#007700">;<br /><br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">() {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">instance </font><font color="#007700">= ++</font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instances</font><font color="#007700">;<br /> }<br /><br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__clone</font><font color="#007700">() {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">instance </font><font color="#007700">= ++</font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instances</font><font color="#007700">;<br /> }<br />}<br /><br />class </font><font color="#0000BB">MyCloneable<br /></font><font color="#007700">{<br /> </font><font color="#0000BB">public $object1</font><font color="#007700">;<br /> </font><font color="#0000BB">public $object2</font><font color="#007700">;<br /><br /> function </font><font color="#0000BB">__clone</font><font color="#007700">()<br /> {<br /> </font><font color="#FF8000">// Force a copy of this->object, otherwise<br /> // it will point to same object.<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">object1 </font><font color="#007700">= </font><font color="#0000BB">clone $this</font><font color="#007700">-></font><font color="#0000BB">object1</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">MyCloneable</font><font color="#007700">();<br /><br /></font><font color="#0000BB">$obj</font><font color="#007700">-></font><font color="#0000BB">object1 </font><font color="#007700">= new </font><font color="#0000BB">SubObject</font><font color="#007700">();<br /></font><font color="#0000BB">$obj</font><font color="#007700">-></font><font color="#0000BB">object2 </font><font color="#007700">= new </font><font color="#0000BB">SubObject</font><font color="#007700">();<br /><br /></font><font color="#0000BB">$obj2 </font><font color="#007700">= </font><font color="#0000BB">clone $obj</font><font color="#007700">;<br /><br /><br />print(</font><font color="#DD0000">"Original Object:\n"</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">);<br /><br />print(</font><font color="#DD0000">"Cloned Object:\n"</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$obj2</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> Original Object:<br />MyCloneable Object<br />(<br /> [object1] => SubObject Object<br /> (<br /> [instance] => 1<br /> )<br /><br /> [object2] => SubObject Object<br /> (<br /> [instance] => 2<br /> )<br /><br />)<br />Cloned Object:<br />MyCloneable Object<br />(<br /> [object1] => SubObject Object<br /> (<br /> [instance] => 3<br /> )<br /><br /> [object2] => SubObject Object<br /> (<br /> [instance] => 2<br /> )<br /><br />)</font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.oop5.final.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.oop5.object-comparison.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Final Keyword</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop5.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Comparing objects</TD ></TR ></TABLE ></DIV ></BODY ></HTML >