2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:17:58 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.oop5.cloning" xmlns="http://docbook.org/ns/docbook">
|
2009-09-10 05:07:22 +00:00
|
|
|
<title>Object Cloning</title>
|
2004-07-10 19:30:37 +00:00
|
|
|
|
|
|
|
<para>
|
2004-07-17 04:05:28 +00:00
|
|
|
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.
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2013-08-16 09:25:19 +00:00
|
|
|
An object copy is created by using the <emphasis>clone</emphasis> keyword (which calls the
|
2012-01-12 15:01:15 +00:00
|
|
|
object's <link linkend="object.clone">__clone()</link> method if possible).
|
|
|
|
An object's <link linkend="object.clone">__clone()</link> method
|
2004-07-10 19:30:37 +00:00
|
|
|
cannot be called directly.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<informalexample>
|
|
|
|
<programlisting>
|
|
|
|
<![CDATA[
|
|
|
|
$copy_of_object = clone $object;
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</informalexample>
|
|
|
|
|
|
|
|
<para>
|
2004-08-07 17:39:27 +00:00
|
|
|
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,
|
2009-03-06 15:23:17 +00:00
|
|
|
will remain references.
|
|
|
|
</para>
|
|
|
|
|
2012-01-12 13:55:24 +00:00
|
|
|
<methodsynopsis xml:id="object.clone">
|
|
|
|
<type>void</type><methodname>__clone</methodname>
|
|
|
|
<void/>
|
|
|
|
</methodsynopsis>
|
|
|
|
|
2009-03-06 15:24:23 +00:00
|
|
|
<para>
|
2012-01-12 15:01:15 +00:00
|
|
|
Once the cloning is complete, if a <link linkend="object.clone">__clone()</link> method is defined, then
|
|
|
|
the newly created object's <link linkend="object.clone">__clone()</link> method will be called, to allow any
|
2009-03-06 15:24:23 +00:00
|
|
|
necessary properties that need to be changed.
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
|
|
|
|
2004-07-17 04:05:28 +00:00
|
|
|
<example>
|
|
|
|
<title>Cloning an object</title>
|
|
|
|
<programlisting role="php">
|
2004-07-10 19:30:37 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2004-10-02 09:40:52 +00:00
|
|
|
class SubObject
|
|
|
|
{
|
|
|
|
static $instances = 0;
|
|
|
|
public $instance;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->instance = ++self::$instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __clone() {
|
|
|
|
$this->instance = ++self::$instances;
|
|
|
|
}
|
2004-08-07 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2004-10-02 09:40:52 +00:00
|
|
|
class MyCloneable
|
|
|
|
{
|
|
|
|
public $object1;
|
|
|
|
public $object2;
|
|
|
|
|
|
|
|
function __clone()
|
|
|
|
{
|
|
|
|
// Force a copy of this->object, otherwise
|
|
|
|
// it will point to same object.
|
2006-12-03 22:09:02 +00:00
|
|
|
$this->object1 = clone $this->object1;
|
2004-10-02 09:40:52 +00:00
|
|
|
}
|
2004-07-10 19:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$obj = new MyCloneable();
|
|
|
|
|
2004-08-07 17:39:27 +00:00
|
|
|
$obj->object1 = new SubObject();
|
|
|
|
$obj->object2 = new SubObject();
|
|
|
|
|
|
|
|
$obj2 = clone $obj;
|
|
|
|
|
2004-07-10 19:30:37 +00:00
|
|
|
|
2004-08-07 17:39:27 +00:00
|
|
|
print("Original Object:\n");
|
|
|
|
print_r($obj);
|
2004-07-10 19:30:37 +00:00
|
|
|
|
2004-08-07 17:39:27 +00:00
|
|
|
print("Cloned Object:\n");
|
|
|
|
print_r($obj2);
|
2004-07-10 19:30:37 +00:00
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
2004-07-17 04:05:28 +00:00
|
|
|
</programlisting>
|
2004-08-07 17:39:27 +00:00
|
|
|
&example.outputs;
|
|
|
|
<screen role="php">
|
|
|
|
<![CDATA[
|
|
|
|
Original Object:
|
|
|
|
MyCloneable Object
|
|
|
|
(
|
|
|
|
[object1] => SubObject Object
|
|
|
|
(
|
|
|
|
[instance] => 1
|
|
|
|
)
|
|
|
|
|
|
|
|
[object2] => SubObject Object
|
|
|
|
(
|
|
|
|
[instance] => 2
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
Cloned Object:
|
|
|
|
MyCloneable Object
|
|
|
|
(
|
|
|
|
[object1] => SubObject Object
|
|
|
|
(
|
|
|
|
[instance] => 3
|
|
|
|
)
|
|
|
|
|
|
|
|
[object2] => SubObject Object
|
|
|
|
(
|
|
|
|
[instance] => 2
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
]]>
|
|
|
|
|
|
|
|
</screen>
|
|
|
|
|
2004-07-17 04:05:28 +00:00
|
|
|
</example>
|
2004-07-10 19:30:37 +00:00
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2004-07-10 19:30:37 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|