Re: __clone() implementation

From: Date: Mon, 02 Feb 2004 18:25:30 +0000
Subject: Re: __clone() implementation
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-7479@lists.php.net to get a copy of this message
At 20:18 02/02/2004, Stephane Drouard wrote:
== Quote from Zeev Suraski (zeev@zend.com)'s article Andi and I have revisited the __clone() implementation and must agree that it wasn't quite right (mainly due to it not working with inheritance). We have rewritten it now (major change!!!) because we didn't want PHP 5 to be released with a fundamentally flawed mechanism. Zeev, Sounds good. However let me precise some troubles with the new implementation. 1/ Imagine a class with members, but it does not implement __clone because the bit for bit copy is OK. Now you derive this class and you need to implement __clone. Because the first class does not implement it, you can't call parent::__clone (I assume). So this is the responsibility of the derived class to clone the base class. This is not only not clean, but private members won't be visible. 2/ It still does not help what I already mentioned: when a class implements __clone(), all its derived classes have also to implement it to transfer their own members. For sure, in these 2 cases there are work arounds: * case 1: modify the base class to implement __clone, * case 2: implement __clone in the derived classes. Another idea for case 1 could be to be able to call parent::__clone even if the class does not implement it and have the bit for bit copy (but just for the base class).
Both are valid points, and they both sound like the same problem from two different angles. I think that the best approach would be to first do an implicit clone (i.e., 'dumb' copy of all of the elements), and then call __clone(), even if it exists. It should solve both cases. Zee

« previous php.internals (#7479) next »