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
$itemis replace the old value is destroyed. The last value of$itemwill remain allocated, but if your code is well structured you’ll be using functions and when$itemgoes out of scope it will finally be destroyed.