1

I´m trying to learn construct and destruct. So, I made this

<?php 

class Numbers {

    public function __construct($numberint,$numbername,$numberletter,$numberpos) {
        $this->numberint = $numberint;
        $this->numbername = $numbername;
        $this->numberletter = $numberletter;
        $this->numberpos = $numberpos;
    }

    public function __destruct() {
        unset($this->numberpos);
    }
}

$number1 = new Numbers(1,"One","A",0);
print_r($number1);

?>

As you can see, I create the class Number and then, use construct for the Objects. But after construct, I want to use destruct, in this case, unset numberpos. I´m trying to put them together to understand how the work. Anyone can help me?

My idea is to change the result:

Numbers Object ( [numberint] => 1 [numbername] => One [numberletter] => A [numberpos] => 0 ) 

To...

Numbers Object ( [numberint] => 1 [numbername] => One [numberletter] => A ) 

Thanks and remember that I´m learning :D

4
  • There seems to be some confusion on your site about what an object is and its relationship to a class. You code does not create objects inside that class. Commented Dec 18, 2015 at 20:05
  • Thanks! I added the WHAT. I hope now is clear. Thanks again! Commented Dec 18, 2015 at 20:06
  • 1
    Destructor is called when object is destroyed and after that object is no more available. So your idea will just not work. Commented Dec 18, 2015 at 20:06
  • @Peter PHP uses a garbage collector. An object gets destructed automatically when there are no variables containing references to it. There's no need to unset properties of the object, because the entire object goes away. The destructor function is only needed if the object contains references to resources outside PHP that need to be cleaned up, e.g. it needs to flush a file buffer. Commented Dec 18, 2015 at 20:42

1 Answer 1

1

The destructor is there to destroy the whole object, not parts of an object. If you want your desired output, you could just do:

$number1 = new Numbers(1,"One","A",0);
print_r($number1);

unset($number1->numberpos);
print_r($number1);

Demo.

If you want to see your destructor getting called, unset the object:

class Numbers {
    public function __destruct() {
        echo "Destructing!\n";
    }
}

$number1 = new Numbers();
unset($number1);

echo "Done!";

Outputs:

Destructing!
Done!
Sign up to request clarification or add additional context in comments.

3 Comments

Ohhhh, great! But if I want to destruct the entire object...? Thanks a lot!
@Peter - The second part of my answer destructs the entire object. You call unset() on the variable that contains your object, in this case $number1.
Thanks a lot! Now I understand! I was thinking that just using the __destruct it would destruct the object. But now, I understand that I have to use unset. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.