note 66165 deleted from language.oop5.cloning by danielc

From: Date: Sun, 27 Jul 2008 05:30:55 +0000
Subject: note 66165 deleted from language.oop5.cloning by danielc
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142801@lists.php.net to get a copy of this message
Note Submitter: olle dot remove_this dot suni at rdnsoftware dot com Reason: 2 ---- As jorge dot villalobos at gmail dot com pointed out, the "__clone is NOT an override". However, if one needs a true copy of an object (which has real copies of all subobjects too, not references), one can define a class method such as this to the object-to-be-cloned: public function copy() { $serialized_contents = serialize($this); return unserialize($serialized_contents); } The method makes a string representation of the object's contents (including subobjects), which can then be unserialized back to an object. The method usage is simple: $original_object = new MyCloneable(); //can have sub objects $copied_object = $original_object->clone(); //only makes copies of the values, not references With serialization, you surely don't have to worry about references, since serialized object is just simple text.

« previous php.notes (#142801) next »