Destroying an ArrayObject in PHP

I searched for the __destroy () method in ArrayObject, but did not find any implementation. If I set the variable containing ArrayObject to NULL, will it correctly destroy all the objects stored in it and free up memory? Or should I repeat the ArrayObject to destroy each of the objects before disabling it?

+4
source share
2 answers

When a ArrayObject is disabled or a zero array, only an ArrayObject instance is destroyed. If ArrayObject contains other objects, they will be destroyed only until there is a link to them from another place, for example.

$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

+2
source

In PHP, you never have to worry about memory usage outside of your own realm. unset($obj) will work fine in your case. Alternatively, you can simply leave the function you are in:

 function f() { $obj = new ArrayObject(); // do something } 

And the data will be cleared just fine.

Managing PHP's internal memory is pretty simple: reference counting is stored for each piece of data, and if it is 0, then it is freed. If only the ArrayObject contains the object, then it has the value 1. When the ArrayObject is gone, refcount is 0, and the object will be deleted.

+2
source

Source: https://habr.com/ru/post/1383198/


All Articles