PHP: Should I disable objects always after using them?

Should I always delete 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 possible to use unset () in this way? Are there better methods for freeing memory after using objects?

+5
source share
3 answers

In your case, no. Each time it $itemreplaces an old value, it is destroyed. The last value $itemwill remain highlighted, but if your code is well structured, you will use functions, and when it $itemgoes out of scope, it will finally be destroyed.

+3
source

? . PHP , , .

+2

No, there is no need for PHP, because objects are automatically destroyed at the end of the process.

+1
source

All Articles