As far as I know (which is very little) , there are two ways, given:
$var = new object()
Then:
// Method 1: Set to null
$var = null;
// Method 2: Unset
unset($var);
Other better method? Am I splitting hairs here?
As far as I know (which is very little) , there are two ways, given:
$var = new object()
Then:
// Method 1: Set to null
$var = null;
// Method 2: Unset
unset($var);
Other better method? Am I splitting hairs here?
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.
unset()
removes the reference to the Object? –
Tara $mp3 = new MP3File($filename);
)? Will unset()
in the end of the block make a difference here? –
Rouen memory_get_usage()
inside that loop as it really depends on what extra things you're doing in the loop and how much memory you actually need. You can't really control when php calls free
and mallocs
. Re-using the variable, in theory, would mean memory space would not have to be re-allocated, symbol table entry would not have to be re-created and corresponding management object. But this is not always the case. –
Basque unset()
make PHP to call __destruct()
magic method? –
Rowles 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.
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/
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
.
In PHP you are not destroying an object, you are destroying a pointer to the object. It is a big difference.
In other languages you can destroy an object and all the other pointers will give you exceptions or rubbish, but it is not a case for PHP.
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
$var2
which was a reference to $var
. Now you destroy $var
as well and, presuming there are no other references holding on the object, you're done. –
Wards 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.
© 2022 - 2024 — McMap. All rights reserved.
new
once, then we must usedelete
once. This is not true in PHP? There is automatic garbage collection when the object is no longer needed? – Hysteroid