I think that this is my first post, i'm not sure.
I've got three files: queue.class.php, node.class.php and test.php, look like this:
queue.class.php:
<?php
include('node.class.php');
class queue
{
public $head;
public $tail;
private $size;
//some methods (e.g __construct)
public function create_node($value){
$data = new Node($value);
return $data;
}
public function push($value){
//here I call create_node
}
//some more methods..
public function __destruct(){
if(!$this->queue_is_empty())
{
$node = $this->head->next;
while(!empty($node))
{
unset($this->head) // Here, doesn't free the memory.
$this->head = $node;
$node = $this->head->next;
}
unset($node);
}
unset($this->head, $this->tail, $this->size);
}
}
?>
node.class.php:
<?php
class Node
{
public $next;
public $previous;
private $data;
//some methods..
public function __destruct(){
unset($this->next, $this->previous, $this->data);
}
}
?>
And in test.php supose that I have the following:
<?php
include('queue.class.php');
$cola = new queue();
for($i = 0; $i < 1000; $i++){
$cola->push($i);
}
unset($cola) //free all the memory;
?>
My doubt is the next: Why when I call unset(node)
inside the "while" of __destruct
(in queue.class.php) doesn't free the memory taken by the node? But if I create a new method inside node.class.php call it, e.g: delete_node()
(with the same code that __destruct
) and replace every unset(node)
with: node->delete_node()
in queue.class.php yes, it works.
I've seen similar themes but I don't understand completely
I hope that understand my explanation and anyone can help me. Thanks
PD: I'm new, i don't know how to do markdown