curt            Sat Aug  7 13:39:27 2004 EDT

  Modified files:              
    /phpdoc/en/language/oop5    cloning.xml 
  Log:
  Be more clear how __clone() works (bug #27100), and example modifications.
  
  
http://cvs.php.net/diff.php/phpdoc/en/language/oop5/cloning.xml?r1=1.2&r2=1.3&ty=u
Index: phpdoc/en/language/oop5/cloning.xml
diff -u phpdoc/en/language/oop5/cloning.xml:1.2 phpdoc/en/language/oop5/cloning.xml:1.3
--- phpdoc/en/language/oop5/cloning.xml:1.2     Sat Jul 17 00:05:28 2004
+++ phpdoc/en/language/oop5/cloning.xml Sat Aug  7 13:39:27 2004
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.2 $ -->
+<!-- $Revision: 1.3 $ -->
  <sect1 id="language.oop5.cloning">
   <title>Object cloning</title>
   
@@ -30,14 +30,13 @@
   </informalexample>
 
   <para>
-   When the developer asks to create a new copy of an object, PHP 5 will check
-   if a __clone() method has been defined or not. If not, it will call a
-   default __clone() which will copy all of the object's properties. If a
-   __clone() method is defined, then it will be responsible to set the
-   necessary properties in the created object. For convenience, the engine
-   will supply a function that imports all of the properties from the source
-   object, so that they can start with a by-value replica of the source
-   object, and only override properties that need to be changed.
+
+   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.
+
   </para>
 
   <example>
@@ -45,34 +44,85 @@
    <programlisting role="php">
 <![CDATA[
 <?php
+
+class SubObject {
+  static $instances = 0;
+  public $instance;
+
+  public function __construct() {
+    $this->instance = ++self::$instances;
+  }
+
+  public function __clone() {
+    $this->instance = ++self::$instances;
+  }
+}
+
 class MyCloneable {
-   static $id = 0;
 
-   function MyCloneable() {
-       $this->id = self::$id++;
-   }
-
-   function __clone() {
-       $this->address = "New York";
-       $this->id = self::$id++;
-   }
+  public $object1;
+  public $object2;
+
+  function __clone() {
+    
+    // Force a copy of this->object, otherwise
+    // it will point to same object.
+    $this->object1 = clone($this->object1);
+  }
 }
 
 $obj = new MyCloneable();
 
-$obj->name = "Hello";
-$obj->address = "Tel-Aviv";
+$obj->object1 = new SubObject();
+$obj->object2 = new SubObject();
+
+$obj2 = clone $obj;
 
-print $obj->id . "\n";
 
-$obj_cloned = clone $obj;
+print("Original Object:\n");
+print_r($obj);
+
+print("Cloned Object:\n");
+print_r($obj2);
 
-print $obj_cloned->id . "\n";
-print $obj_cloned->name . "\n";
-print $obj_cloned->address . "\n";
 ?> 
 ]]>
    </programlisting>
+   &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>
+
   </example>
 
  </sect1>

Reply via email to