+ Reply to Thread
Results 1 to 8 of 8

Thread: PHP 5 OOP Destructors (yuck)

  1. #1
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    PHP 5 OOP Destructors (yuck)

    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
    destructing b
    destructing a
    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 by garrettroyce; 05-27-2009 at 04:44 PM. Reason: Automerged Doublepost
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP 5 OOP Destructors (yuck)

    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 Code:
    $this->b->__destruct(); 
    part, and everything goes on fine.
    Last edited by xav0989; 05-27-2009 at 05:16 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP 5 OOP Destructors (yuck)

    Nicely done

    I was just checking your knowledge, I really didn't spend an hour trying to figure that out ;)
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP 5 OOP Destructors (yuck)

    Anyways, it's always a pleasure to share knowledge, and thanks for the credits
    Last edited by xav0989; 05-28-2009 at 06:31 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP 5 OOP Destructors (yuck)

    Quote Originally Posted by xav0989 View Post
    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.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP 5 OOP Destructors (yuck)

    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.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  7. #7
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP 5 OOP Destructors (yuck)

    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
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  8. #8
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP 5 OOP Destructors (yuck)

    I've always been a fan of OOP, as it permits you to group similar functions under a common context.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. PHP Easter Eggs
    By dragoneye_xp in forum Off Topic
    Replies: 3
    Last Post: 06-14-2006, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers