Web Hosting Talk







View Full Version : How to copy an object variable (Copy. not reference)


grabmail
04-28-2006, 06:24 AM
if i do this:

$arr = $obj->arr

$arr = null;

I will make $obj->arr null since by default, php 5 reference obj variables.

But i do not want to refer to it. I want to make a copy of it. How do i do so?

deuce868
04-28-2006, 08:15 AM
Clone

http://us2.php.net/manual/en/language.oop5.cloning.php

bigfan
04-28-2006, 09:25 AM
I will make $obj->arr null...Doesn't seem to be so:
class Test
{
public $arr = array('hello');
}

$obj = new Test;
$arr = $obj->arr;
$arr = null;
echo $obj->arr[0];

// Echos 'hello'

grabmail
04-28-2006, 12:14 PM
hmmm... you're right bigfan. it turns out that my magic method __get was wreaking havoc. thanks for the insight.