tim-tim Posted November 21, 2006 Share Posted November 21, 2006 HiI'm running v 5.1.6. The script I've included below is an emulation of the problem I'm having, except that it doesn't actually exhibit the problem that I'm having in the real program, but bear with me please.The values of the object stored in the ar[0] element change when the anotherAr[0] changes, unless of course I keep in the 'clone' keyword as I have it below, which is really the solution to the problem.But the problem is that in the real program the clone keyword has no affect at all, and I can see clearly that this is the case (changes to the anotherAr[0] are also viewed in ar[0] - well in their replacements in the progam).I don't want to include the whole program I'm working on,So my question is, what is it that could stop the clone keyword from actually making a copy of an object in the sort of case below? E.g: The presence of subclasses in the object to be cloned or something like that (although as you can see I've tested that theory below and it doesn't affect it)? Can anyone answer this theoretical question?I haven't made a clone method in my program, just as I haven't made one in the example below.Tim[code]<?php class WantToBeCopied { protected $name; protected $value; function setName($name) { $this->name = $name; } function setValue($value) {$this->value = $value;} function __toString() { return 'name: ' . $this->name . " value : " . $this->value; }}class Test { public $ar; function t() { $this->ar[0] = new WantToBeCopied(); $this->ar[0]->setName('original'); $this->ar[0]->setValue('original value'); $anotherAr[0] = clone $this->ar[0]; $anotherAr[0]->setName('new name'); $anotherAr[0]->setValue('new value'); echo '<pre>'; echo "original: " . $this->ar[0]->__toString() . "\n"; echo "another: " . $anotherAr[0]->__toString() . "\n"; }}$t = new Test();$t->t();?>[/code] Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted November 22, 2006 Share Posted November 22, 2006 $anotherAr[0] may be creating a reference link to the original... look for the &= "assignment" operator. Quote Link to comment Share on other sites More sharing options...
tim-tim Posted November 22, 2006 Author Share Posted November 22, 2006 I haven't used any references if thats what you mean? I understand that the problem is initially from the array elements wanting to be assigned by reference (well I assume that from the results). Thats why I need to make the clone work.Can anyone tell me how to see the memory address of variables?ThanksTim Quote Link to comment Share on other sites More sharing options...
Jenk Posted November 22, 2006 Share Posted November 22, 2006 I'm not experiencing this behaviour:[code]original: name: original value : original valueanother: name: new name value : new value[/code] Quote Link to comment Share on other sites More sharing options...
tim-tim Posted November 22, 2006 Author Share Posted November 22, 2006 Ja, as I said, that was just an example script that does actually work (I couldn't manage to emulate the problem), so my question was theoretically what can stop that cloning from working properly.I just managed to solve the problem though, I'm still not exactly sure when the a custom __clone method needs to be done or what it needs to do, but I realized that one thing I needed to do was make a copy of the array that was stored in WantToBeCopied ( I know there isn't an array in the example script below that because I wasn't sure what I needed to emulate the problem).Anyways I've realized that the internal clone process just copies arrays as is without making a copy of their contents, that is what my problem was, it would be nice to know exactly what else would need to be done in the __clone functions, I have a feeling that sub objects also need to specifically copied in a custom __clone method. The manual isn't very clear at all about what the internal clone method does, and for those who don't know the internal method always executes when using 'clone ThisObject' and then your custom __clone method has to fix what the internal clone method has not done correctly, like copy the values of an array or a subobject in the class.Thanks guysTim Quote Link to comment Share on other sites More sharing options...
Jenk Posted November 22, 2006 Share Posted November 22, 2006 __clone is for setting proprietry properties upon cloning. It is called on the new object, after the object is cloned, not on the old object prior to cloning.It's useful if you have any references within an object so you can refresh those objects.[code]<?phpclass Foo{ public $bar; public function __construct() { $this->bar = new SomeClass(); } public function __clone() { $this->bar = clone $this->bar; }}?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.