1

Is there any purpose to unset $data? Should i unset it if it contains large data?

<?php
require 'db.php';
class Test{
    public $id = 0;
    public $name;
    public function __construct()
    {
        $this->getUserInfo();
        echo $this->name;
    }
    private function getUserInfo()
    {
        global $db;
        $query = $db->prepare('SELECT id,name FROM users WHERE group = :g LIMIT 1');
        if ($query->execute(array('g' => 'admin')))
        {
            $data = $query->fetch(); // <--
            $this->id = $data[0];
            $this->name = $data[1];
            return true;
        }
    }
}
(new Test);
?>
Tufan
  • 25
  • 5
  • Did not now witch php version you use. But sometimes in some php version local variables arent cleared corrrect by the grabage collector so if you want to be sure do `$data=null;unset($data);`. – JustOnUnderMillions Jan 18 '17 at 16:27
  • Here is many to read about that topic: http://stackoverflow.com/questions/2461762/force-freeing-memory-in-php read comment too to get an overview. – JustOnUnderMillions Jan 18 '17 at 16:31
  • @JustOnUnderMillions Thanks, i'm using PHP 7.0.9. I think it'll clean it correctly. – Tufan Jan 18 '17 at 16:35
  • FasterThanLight PHP oh ok. Then, dont worry. But may read about references and references of references in php. – JustOnUnderMillions Jan 18 '17 at 16:37

1 Answers1

3

There's no need. When the function returns, the variable goes away by itself.

And even if you do unset it, you still have references to the values that it contained in $this->id and $this->name, so their memory won't be reclaimed. The only memory you'll reclaim is the tiny array object that points to them.

PHP doesn't make copies when you do assignments. Strings and numbers are immutable, so there's no need to copy them. Objects are copied by reference. And arrays use copy-on-write technology, so it only copies them later if the old reference still exists and you then modify the copy.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • To get it clear `$this->id = trim($data[0]);` prevents an reference at this point? – JustOnUnderMillions Jan 18 '17 at 16:32
  • Yes, because that creates a new string. – Barmar Jan 18 '17 at 16:33
  • So we can say `everthing` is a reference in php as long you are not changing the content/value of the new variable. Would it not be a little saver to do something like `$data=null;` to prevent refrences of references in complex codes!? – JustOnUnderMillions Jan 18 '17 at 16:36
  • You might do that for global variables. It's rarely necessary for local variables inside a function. – Barmar Jan 18 '17 at 16:39
  • Sounds right. But im working in the land of legacy code for years :\ And the `land of legacy` can really get ugly.... so i always try to take the save way. – JustOnUnderMillions Jan 18 '17 at 16:42
  • 1
    Unless your script is running for a long time, it's probably not important at all. Everything goes away when the script finishes. I've never bothered clearing a variable in a script that's just responding to a web request. – Barmar Jan 18 '17 at 16:44
  • Im doing large data imports with jobs that are running up to 6 hours :) Thnx for reply – JustOnUnderMillions Jan 18 '17 at 16:45
  • 1
    If you're using a loop that processes each item, each time you read a row you'll replace the variables that hold the data from the previous row. Unless the code is doing something strange, it's unlikely that any large data will persist for a long time. – Barmar Jan 18 '17 at 16:49