Will this be a PHP script memory leak?

I have a PHP script that runs in the background for a while (usually a few minutes, but it can be up to an hour or so). It contains a loop in which I need to create an object. I am currently using the same name:

while (!$job_finished) {
    $x = new MyClass();
    $x->doStuff();
    $x->doMoreStuff();
    unset ($x);

    // more code here
}

Since I create $ x with the same name many times, will garbage collection properly clear memory? Or should I use an array over $ x, for example

   $x[$i] = new MyClass();
+4
source share
2 answers

Actually I do not need to use an array. The unset () command will destroy the object, and so I should not worry. This is in the PHP documentation where it says:

, .

, , destruct script.

<?php

class A {
  function __destruct() {
    echo "cYa later!!\n";
  }
}

$a = new A();
unset($a);

echo "hello";
sleep(10);
0

, , .

, , .

-1

All Articles