How to destroy an object?

后端 未结 6 1114
挽巷
挽巷 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.

提交回复
热议问题