Jump to content

Arrays, reference, objects, cloning


tim-tim

Recommended Posts

Hi

I'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]
Link to comment
Share on other sites

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?

Thanks
Tim
Link to comment
Share on other sites

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 guys
Tim
Link to comment
Share on other sites

__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]<?php

class Foo
{
    public $bar;
    public function __construct()
    {
        $this->bar = new SomeClass();
    }

    public function __clone()
    {
        $this->bar = clone $this->bar;
    }
}

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.