Search code examples
phparrayobject

Destroying an ArrayObject in PHP


I was looking for the __destroy() method in ArrayObject but found no implementation. If I set the variable containing an ArrayObject to NULL, will it correctly destroy all the objects stored in it and free memory? Or should I iterate the ArrayObject to destroy each of the objects before unsetting it?


Solution

  • When you unset or null the ArrayObject only the ArrayObject instance is destroyed. If the ArrayObject contains other objects, those will only be destroyed as long as there is no reference to them from somewhere else, e.g.

    $foo = new StdClass;
    $ao = new ArrayObject;
    $ao[] = $foo;
    $ao[] = new StdClass;
    $ao = null;     // will destroy the ArrayObject and the second stdClass
    var_dump($foo); // but not the stdClass assigned to $foo
    

    Also see http://www.php.net/manual/en/features.gc.refcounting-basics.php