Do I need to destroy objects in PHP after using them?

How critical is object destruction in PHP? Is it important to destroy objects in PHP after using them? because unlike java, PHP does not have a garbage collector (nothing I know)

+5
source share
5 answers

You do not need to destroy objects in the general case, and PHP certainly has a garbage collector . Moreover, most simple scripts do not even need this, because the whole environment is broken down and rebuilt for each HTTP request; The garbage collector helps those scripts that run out of memory while serving a single request.

Exceptions to the general case:

, "" , / ; ,

$largeObject = null; // reference to previous value lost

$largeObject, :

, " , ".

+13

, . , .

http://paul-m-jones.com/archives/262

, .

function __destruct()
{
    //do stuff
}

, , script.

+1

Garbage Collection ( GC), PHP 5.3.

<?php
    gc_enable(); // Enable Garbage Collector
    var_dump(gc_enabled()); // true
    var_dump(gc_collect_cycles()); // # of elements cleaned up
    gc_disable(); // Disable Garbage Collector
?>

, !:)

0

Php , , php , unset . singleton, xml, . php unset . - .

0

According to others, the garbage collector will do this work at the end of execution, however, if you have a long work cycle, you may run into problems if / when you work with large volumes of data or huge arrays.

The solution at this stage is to use unset ($ object), which calls the magic __destruct () function, which you can use if you want to do something with the object (for example, save data in a database).

0
source

All Articles