Should I always unset objects after using them? Consider the following code.
foreach ( $items as $item_id )
{
$item = new Item($item_id);
echo $item->name;
unset( $item );
}
Is it advisable to use unset() this way? Are there better techniques to free up memory after using objects?
In your case, no. Everytime $item
is replace the old value is destroyed. The last value of $item
will remain allocated, but if your code is well structured you'll be using functions and when $item
goes out of scope it will finally be destroyed.
Always? No. PHP has a garbage collector which will take care of removing objects from memory once they are not being used any longer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With