note 66165 added to language.oop5.cloning

From: Date: Tue, 16 May 2006 11:49:22 +0000
Subject: note 66165 added to language.oop5.cloning
Groups: php.notes 
Request: Send a blank email to php-notes+get-111377@lists.php.net to get a copy of this message
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. ---- Server IP: 66.163.161.117 Probable Submitter: 62.165.130.166 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.cloning.php Edit -- http://master.php.net/note/edit/66165 Del: integrated -- http://master.php.net/note/delete/66165/integrated Del: useless -- http://master.php.net/note/delete/66165/useless Del: bad code -- http://master.php.net/note/delete/66165/bad+code Del: spam -- http://master.php.net/note/delete/66165/spam Del: non-english -- http://master.php.net/note/delete/66165/non-english Del: in docs -- http://master.php.net/note/delete/66165/in+docs Del: other reasons-- http://master.php.net/note/delete/66165 Reject -- http://master.php.net/note/reject/66165 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111377) next »