PHP 5 OOP Destructors (yuck)

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
This code:
Code:
<?php
abstract class a {
    public function __construct () {
        echo 'constructing ', __CLASS__, '<br/>';
    }
    public function __destruct() {
        echo 'destructing ', __CLASS__, '<br/>';
    }
}
class b extends a {
    public function __construct() {
        echo 'constructing ', __CLASS__, '<br/>';
        parent::__construct();
    }
    public function __destruct() {
        echo 'destructing ', __CLASS__, '<br/>';
        parent::__destruct();
    }
}
class d {
    public function __construct() {
        echo 'constructing ', __CLASS__, '<br/>';
        $this->b = new b();
    }
    public function __destruct() {
        echo 'destructing ', __CLASS__, '<br/>';
        $this->b->__destruct();
        unset($this->b);
    }
    public $b;
}

new d();
?>
Produces this output:
Code:
constructing d
constructing b
constructing a
destructing d
destructing b
destructing a
[B]destructing b[/B]
[B]destructing a[/B]
The problem being the two last lines of output. The problem is related to d::b. Removing d::b causes everything to destruct correctly, but also completely ruins the functionality of what I'm trying to do :p

It won't hurt my program to have b and a destruct twice, as long as they destruct after d, but I'd like to know why, if possible :)
Edit:
And for your time, 332 credits to whomever solves it :) (and some rep)
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
when you call the __destruct function, you simply execute the code inside, but you are not actually removing the class. However, when you do the unset, the __destruct code is executed automagically, so no need to be called before, then the object is removed from the memory.

So if not in a context where the object is unloaded, the __destruct function is just a normal function, just a any other function. Remove the
PHP:
$this->b->__destruct();
part, and everything goes on fine.
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Nicely done :)

I was just checking your knowledge, I really didn't spend an hour trying to figure that out ;)
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Anyways, it's always a pleasure to share knowledge, and thanks for the credits
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Anyways, it's always a pleasure to share knowledge, and thanks for the credits

Money well spent :p

I think my problem was I spent too much time reading user comments in the PHP documentation. Half of them are wrong so it makes it pretty hard to figure out what's going on.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I usually don't read it. If I have a question that the doc can't answer I:
* Search google for tutorials, examples or docs
* Search for projects that implement what I am trying to do and inspire myself
* Ask for advice on the forums.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Yeah, I tried to find a good PHP5 OOP tutorial, but I couldn't find anything really worthwhile. It seems like PHP OOP is taking a while for a lot of people to warm up to. I know that there's plenty of really sharp programmers here, so I figured I'd go straight to the experts :)
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I've always been a fan of OOP, as it permits you to group similar functions under a common context.
 
Top