Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use unset in php __destruct()?

Tags:

oop

php

Is it a good practice to unset the variables that you used in a class? Or its an optional?

If its a good practice what is the benefit of using the unset function?

like image 461
jjz Avatar asked Feb 12 '10 10:02

jjz


People also ask

What's better at freeing memory with PHP unset () or $var Null?

It seems that $a = null is a bit faster than its unset() counterpart: updating a symbol table entry appears to be faster than removing it.

What is the use of unset in PHP?

The unset() function in PHP resets any variable. If unset() is called inside a user-defined function, it unsets the local variables. If a user wants to unset the global variable inside the function, then he/she has to use $GLOBALS array to do so. The unset() function has no return value.

Is unset variable used in PHP?

PHP | unset() Function The unset() function is an inbuilt function in PHP which is used to unset a specified variable.

How do you unset multiple values in PHP?

if you see in php documentation there are not available directly remove multiple keys from php array. But we will create our own php custom function and remove all keys by given array value. In this example i created custom function as array_except().


1 Answers

You really don't need to worry about cleaning up your variable declarations in PHP, its garbage collection takes care of all of that for you. Your __destruct() methods are primarily for things like closing persistent connections.

like image 193
nortron Avatar answered Nov 09 '22 20:11

nortron