views:

40

answers:

2

My class has a member variable array, items. Periodically I reassign the array to be the value of another, temporary array, like this:

$temp = array();
$temp[] = new Object();
$temp[] = new Object();
$temp[] = new Object();
... etc.

$this->items = $temp;

So, could I have a memory leak? By reassigning the value of $this->temp to a new value, $temp, would all the items (the items are objects) originally in $this->temp still linger around, or would they be freed?

+1  A: 

They will linger around for a little while, but in PHP they will be freed eventually by the garbage collector.

BlueRaja - Danny Pflughoeft
This is also the case in Java, C#, and other memory-managed languages. Note that this is **not** the case in C/C++, which have no garbage collector; any memory allocated by `new` (`malloc()`) must later be freed by `delete` (`free()`)
BlueRaja - Danny Pflughoeft
+2  A: 

This will not cause a memory leak. $temp and $this->items are just references to the same array. Since PHP is a garbage collected language, the array will be deleted (garbage collected) when there are no more references to the array.

driis