Jump to content

destruct call order?


Koobazaur

Recommended Posts

Hey,

 

I have several classes in my program that do various thing in their destructors. Problem is, some depend on each other, so if the destructors are not called in a proper order, then I get errors since one object is trying to do something (in its destructor) with an object that was already destroyed.

 

An example of this is my Database which a lot of objects rely on; I wanted to have it connect in constructor and disconnect in destructor (instead of connecting/reconnecting several times through the script), but due to the above mentioned issue, this solution produces errors.

 

Is there any way to control when destructors get called? Or do I need to use a workaround? One solution I thought of was to make my objects members of another object and have that object call the constructors/destructors manually in proper order in its own construct/destruct functions. This solution is a pain, however, as it means I need to practically re write large portions of my code ><

Link to comment
Share on other sites

An objects destructor is called when all references to the object are removed. If one object holds a reference to another, the latter will not be destructed before the the former. Since destructors can't take any arguments, I assume you've setup global access to the latter instead of using a property? When an object is in the global scope, it's destructor is called during script shutdown. I am not aware of any ordering rules in such a case, but even if there were, this is not something you should depend on.

 

You might want to post some code or a UML diagram, so we can suggest some concrete changes.

Link to comment
Share on other sites

Objects that depend on each other get their reference passed to them in the constructor (I also ensure they are created in a proper order).

 

So for instance:

 

$MyDatabase = new cDatabase(Username, Password ...);

$MyObject = new cObject($MyDatabase, Other settings...);

 

When I set $MyDatabase to connect itself in a constructor, and disconnects itself in the destructor, then my $MyObject, which uses it in its own destructor, throws an error.

Link to comment
Share on other sites

You're not making sense. You're saying you're passing an object through a constructor but NOT storing it's reference in a property and then attempt to use it in the destructor? If so: no wonder you get an error.

 

You misunderstood, I AM storing its reference. Here's how it basically works:

 

class a
{
private $rB;

function __construct(&$rObjectB)
	{
	 $this->SetB($rObjectB);
	}		

function SetB(&$rObjectB);
	{
	$this->$rB = &$rObjectB;
	}		

function __destruct()
	{
	$this->$rB->DoSomething();
	}
}


$ObjectA = new a($ObjectB);

 

(note: I know using SetB() seems redundant, but I do it this way since my class inherits from another, and its the parent that sets that reference, so I need to call the parents function to set the variable)

Link to comment
Share on other sites

One solution I thought of was to make my objects members of another object ...

 

Hence the confusion. Did you mean creating a new class, responsible for calling the destructors? That might not be a bad idea, depending on the type of objects (although I would use regular methods). If were talking persistent objects you could have a Data Mapper store references in an Identity Map, and let the Mapper do the final manipulation of each object.

 

Otherwise, you might have a case of circular reference, because normally an aggregate object isn't destructed before the whole. Please check if you are initializing all your properties as well. I recall neglecting to do so on PHP4 can cause unexpected behaviour.

 

In any case, it is my experience that you shouldn't depend too much on destructors.

 

How about showing us the actual code, and the error you are getting?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.