How to destroy an object?

后端 未结 6 1089
挽巷
挽巷 2020-11-29 01:54

As far as I know (which is very little) , there are two ways, given:

$var = new object()

Then:

// Method 1: Set to null
$va         


        
相关标签:
6条回答
  • 2020-11-29 02:36

    You're looking for unset().

    But take into account that you can't explicitly destroy an object.

    It will stay there, however if you unset the object and your script pushes PHP to the memory limits the objects not needed will be garbage collected. I would go with unset() (as opposed to setting it to null) as it seems to have better performance (not tested but documented on one of the comments from the PHP official manual).

    That said, do keep in mind that PHP always destroys the objects as soon as the page is served. So this should only be needed on really long loops and/or heavy intensive pages.

    0 讨论(0)
  • 2020-11-29 02:43

    A handy post explaining several mis-understandings about this:

    Don't Call The Destructor explicitly

    This covers several misconceptions about how the destructor works. Calling it explicitly will not actually destroy your variable, according to the PHP5 doc:

    PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

    The post above does state that setting the variable to null can work in some cases, as long as nothing else is pointing to the allocated memory.

    0 讨论(0)
  • 2020-11-29 02:43

    May be in a situation where you are creating a new mysqli object.

    $MyConnection = new mysqli($hn, $un, $pw, $db);

    but even after you close the object

    $MyConnection->close();

    if you will use print_r() to check the contents of $MyConnection, you will get an error as below:

    Error:
    mysqli Object
    
    Warning: print_r(): Property access is not allowed yet in /path/to/program on line ..
    ( [affected_rows] => [client_info] => [client_version] =>.................)
    

    in which case you can't use unlink() because unlink() will require a path name string but in this case $MyConnection is an Object.

    So you have another choice of setting its value to null:

    $MyConnection = null;

    now things go right, as you have expected. You don't have any content inside the variable $MyConnection as well as you already cleaned up the mysqli Object.

    It's a recommended practice to close the Object before setting the value of your variable to null.

    0 讨论(0)
  • 2020-11-29 02:49

    Short answer: Both are needed.

    I feel like the right answer was given but minimally. Yeah generally unset() is best for "speed", but if you want to reclaim memory immediately (at the cost of CPU) should want to use null.

    Like others mentioned, setting to null doesn't mean everything is reclaimed, you can have shared memory (uncloned) objects that will prevent destruction of the object. Moreover, like others have said, you can't "destroy" the objects explicitly anyway so you shouldn't try to do it anyway.

    You will need to figure out which is best for you. Also you can use __destruct() for an object which will be called on unset or null but it should be used carefully and like others said, never be called directly!

    see:

    http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/

    What is difference between assigning NULL and unset?

    0 讨论(0)
  • 2020-11-29 02:56

    This is a simple prove that you cannot destroy an object, you can only destroy a link to it.

    $var = (object)['a'=>1];
    $var2 = $var;
    $var2->a = 2;
    unset($var2);
    echo $var->a;
    

    returns

    2

    See it in action here: https://eval.in/1054130

    0 讨论(0)
  • 2020-11-29 02:57

    I would go with unset because it might give the garbage collector a better hint so that the memory can be available again sooner. Be careful that any things the object points to either have other references or get unset first or you really will have to wait on the garbage collector since there would then be no handles to them.

    0 讨论(0)
提交回复
热议问题