SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __destruct called in weird place

    Is there any reason __destruct would be called when an object gets returned from another object and assigned to a class variable which is an array? (classic factory/registry setup).

    It seems to get called at this point where its assigned.

    $this->data[$class] = $this->factory->$constructor($this);

    Where $this->factory->$constructor($this) is returning the object where __destruct is getting called at the line above.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm...what if the object being returned was going out of scope when the method returns. Like maybe a copy of it existed?

  3. #3
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought with PHP5 when returning/passing objects they were always by reference now (no copies)

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but I was suggesting maybe you had another object, and were confusing the __destruct call as being called on the object that was returned, when it was called on the one which went out of scope.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure what you mean.

    In the above...
    $this->data[$class] = $this->factory->$constructor($this);

    This assigns the object to $this->data[$class] so it shouldn't destruct.

  6. #6
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you mean... however we never have two copies of a database object. Its always stored in the registry.

    Since PHP5 assigns all objects as references why does it matter if one goes out of scope if they all reference the main object in the registry which never goes out of scope until the end of the script processing.

    If a singleton copy of the registry is used (say inside a function) and the function ends (goes out of scope) is this going to destroy the registry as well?

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Show us the code, then we can see what you're on about.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, consider trying to mock up the simplest peice of code you can which will exibit this behavior.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •