For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > April 2007 > how to clone a class?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to clone a class?
_mario.lat

2007-04-21, 6:57 pm

Hallo,
I have a class A to manage a grout of classes B.
(for example "Class Garden" and "Class Flower" which are in garden)
If i clone A (Garden) I'd like to clone also B (Folwers).
How cn I do?
thank you in advance,
Mario
C.

2007-04-22, 9:57 pm

On 21 Apr, 22:16, "_mario.lat" <n...@libero.it> wrote:
> Hallo,
> I have a class A to manage a grout of classes B.
> (for example "Class Garden" and "Class Flower" which are in garden)
> If i clone A (Garden) I'd like to clone also B (Folwers).
> How cn I do?
> thank you in advance,
> Mario



Presumably you've got references to flower objects in your garden
instance - the simplest way to get a deep clone is to serialize it:

$deep_clone=unserialize(serialize($garde
n));

There may be a smarter to do it - and of course the above may break if
you've got _sleep to lose the references.

C.

ZeldorBlat

2007-04-22, 9:57 pm

On Apr 21, 5:16 pm, "_mario.lat" <n...@libero.it> wrote:
> Hallo,
> I have a class A to manage a grout of classes B.
> (for example "Class Garden" and "Class Flower" which are in garden)
> If i clone A (Garden) I'd like to clone also B (Folwers).
> How cn I do?
> thank you in advance,
> Mario


Look at this:

<http://www.php.net/manual/en/language.oop5.cloning.php>

In particular, you need to define a __clone() method in your Garden
class. This method should, in turn, use the clone operator on all of
the Flowers in the Garden.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com