Re: Memory/Object question
| From: | Max Derkachev | Date: | Fri, 10 Nov 2000 08:45:31 +0000 |
| Subject: | Re: Memory/Object question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24635@lists.php.net to get a copy of this message | ||
Bug Hunter wrote:
> If I have an object in an array and do the following:
>
> obj2 = new obj;
>
> obj["1"] = obj2;
>
> If obj["1"] already existed, what happens to it? does it lay around
> consuming memory, or is it destroyed?
>
> Or does the data in the object get copied? If not, can I make the data
> get copied so I don't eat up memory, without doing a variable at a time?
First, I guess you meant $obj2, $obj[1] and obj in your snippet, cause variables
begin with $ in php .
The data get copied. At least in current versions of php3 and php4.
That means you will get two separate objects of the class obj,
$obj[1] will be a copy of $obj2, and hense you'll eat twise as much memory.
If you change something in $obj[1], it won't affect $obj2.
If you want to deal with one instance of the class obj, references would help:
$obj2 = new obj;
$obj[1] = &$obj2;
Now you have a reference to $obj2 in $obj[1], not a copy of $obj2, and if you deal
with $obj[1], you'll actuelly deal with $obj2.
--
Best regards,
Max A. Derkachev mailto:kot@books.ru
Symbol-Plus Publishing Ltd.
phone: +7 (812) 265-0054, 265-1228, phone/fax: 567-8775
http://www.Books.Ru -- All Books of Russia