3

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

Sheed
  • 577
  • 4
  • 18
  • 7
    As a general piece of guidance, try not to compare languages like this. They have completely different rules, properties and mechanisms, so you'll just confuse yourself. The [manual](http://php.net/manual/en/language.oop5.decon.php) has some examples of what you can do in a PHP destructor. – Lightness Races in Orbit Mar 20 '18 at 11:36
  • 2
    It's fairly rare for destructors to be used in PHP - the language will do a good job of garbage collection without any help. In my experience they're most commonly used for adding debugging/logging code. [This question](https://stackoverflow.com/questions/7240230/is-the-destruct-method-necessary-for-php) has some answers that might be useful. For reference, `delete` isn't valid PHP, so I'm not sure where you saw that. – iainn Mar 20 '18 at 11:45

1 Answers1

3

What do you write in the destructor in C++?
You probably write statements that release the resources acquired during the lifetime of the object, isn't it?

This is the same in PHP, but most of the times you don't need to put anything in the destructor because the unused memory and resources are automatically garbage-collected by the interpreter.

It is recommended, however, to release in the destructor the resources acquired in the constructor (or during the lifetime of the object) either to be sure the memory they use is freed earlier or as documentation: somebody that reads a destructor and finds a call to fclose() in it knows that in the constructor or maybe in another method of the class, a pairing call to fopen() was executed and the handler was stored inside the object.


P.S.

  1. The line $n from:

    class A3{
        $n
        public function __deconstruct(){
    

    is incorrect. It should end with a semicolon (;) and if the intention is to declare a property the correct syntax is to start with one of the visibility specifiers (private, protected or public), like this:

    class A3 {
        private $n;
        public function __destruct() {
    
  2. The name of the destructor method is __destruct() and not __destructor().

  3. There is no delete in PHP: http://php.net/manual/en/function.delete.php. Your three choices are only one: choice #1 (unset($this->pdo);).

Read more about classes and objects in PHP. Start by forgetting most of what you know from C++ as PHP OOP works in a different way and your knowledge of C++ will probably make more harm than good on learning OOP in PHP.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • thanks, I saw the delete function in a post and I didn't suspect it was wrong since the OP was pretty assertive, and seeing a php.net "delete" reference title I didn't question further – Sheed Mar 20 '18 at 12:19
  • Bookmark the official [PHP documentation](http://php.net/manual/en/) and use it as the first place you search for help regarding PHP and the place where you return to verify claims you find and things that are not explained in SO answers. – axiac Mar 20 '18 at 12:39