I have Created following class in flie index.class.php
:-
<?php
class index{
public function loadTitle() {
$title = "Welcome to My Website";
return $title;
}
}
?>
And now, I am using it like below in my index.php
file
<?php
require_once("index.class.php");
$obj = new index();
echo $obj->loadTitle();
?>
My question because the Page will become heavy with lots of article and images, and I will expect 1500-2500 user every day in it.
Do I need to also unset memory, I know PHP has its own garbage collector, but following URL scares me out :- www.google.com/search?q=php+memory+leak+with+new
and I saw a few questions in StackOverflow saying it does consumes memory, and certain people have suggested to use the unset
function, but I am not sure how to do it..
This is my attempt:
Do I need to only call
<?php
unset($obj); // At the end of the page, or where i will no more be using this object.
?>
Or I have to set NULL value too.
<?php
$obj = NULL;
unset($obj);
?>
Is the above code fine for releasing memory, or do I need to do something else too? Please suggest and teach me!
Firstly, you don't need to unset your variables all the time. I've seen people unset ALL their variables at the end of their PhP script, which is useless.
The only time where you'd want to unset variables is if you've memory problems in PhP and you can destroy variables you won't use anywhere else in your script, but you still have a lot of things to do (considering time or memory).
You might also want to give a look at :
PHP has automatic garbage collection. Every time you reassign $obj
, if the old value is no longer accessible, its memory will be reclaimed. So you'll only ever have a reference to one object, not all the ones you ever created, unless you store them somewhere.
You should look into the garbage collector of PHP. If you want, you can even force PHP to collect the garbage:
<?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
?>