tags:

views:

503

answers:

3

Is there a way in PHP to destroy an object from within that same object?

A: 

No, I don't think there is. But would you need such thing?

RaYell
+10  A: 

If a method is called in the object's context then there has to be at least one reference to that object. And since php only removes unreachable objects the answer is: no.

VolkerK
+1  A: 

There is a way to self destruct an object : Use the $GLOBALS array to find your instance in it, then use unset(). Be aware that unset() does not automatically call the __destruct() magic method all the time... There is such a note in this way (see the unset() documentation) in the PHP documentation, but it does not explain exactly when unset() does not call the __destruct() method. And I had this specific behaviour : I do a : unset($myInstance); $myInstance = clone $otherInstance; And the __constructor is called first, then the __destruct(). Or I would like the __destruct() to be called first because unset() is before clone... I ma stuck with that now...

Nicolas.

sauvaget