I have a hard time understanding what SHOULD be done in a PHP class destructor
I'm coming from c++. In c++, if I have this:
class A{
int n;
~A(){
}
}
class A2{
int* n;
~A2(){
delete n;
}
}
The language KNOWS that if an instance of A is out of scope, its member n should be deallocated, because n only belongs to that very instance. BUT if an instance of A2 goes out of scope, it doesn't knows if the memory pointed by n should be deallocated (maybe there are other pointers out there pointing towards that same memory) BUT, if we're sure we want to deallocate that memory IF A2 instance goes out of scope, we need to MANUALLY "delete" it, because its our intention.
What about php? I'm a newcomer and I've seen several solutions:
class A3{
private $n;
public function __destruct(){
//Choice 1
unset($this->$n);
//Choice 2
delete $this->$n;
//Choice 3
}
}
I'm not sure of the difference between unset and delete, but whatever. I was told (cf choice 3) that PHP does "by itself" deallocate the memory if we don't do anything. But I don't then understand the use of delete or unset in the destructor. We don't have the notion of "pointer" in php, so if an instance of A3 goes out of scope, it's natural to deallocate n.
so, is there anything that SHOULD be done in a destructor? If not, there is never a use of delete or unset in the destructor?
EDIT: rewritten code based on axiac comment