Re: Destructor Order

From: Date: Wed, 22 Oct 2008 09:21:24 +0000
Subject: Re: Destructor Order
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-41316@lists.php.net to get a copy of this message
On Wed, Oct 22, 2008 at 5:03 AM, Ryan Panning <rpanning@gmail.com> wrote:
I've been wondering, is such a thing even possible? Is there a good way to implement an object destruct order? Here are my thoughts: In the class definition, specify what "level" of destruction the objects should be on. How, I have no idea, I haven't thought of a good syntax. It should be an integer for what level though. Then when the script ends, the engine starts with the highest most level of destruction. It continues down until everything has been destructed. With the last most level being objects with unspecified levels. Note: Each level can have more than one class. Example destruction order: 3 = database records (ActiveRecord or such) 2 = database connection object 1 = framework objects 0 = objects with unspecified level
Hi, This would likely (and possibly rightfully) be frowned upon, but according to the current codebase, if the destructor creates a new reference outside to the destroyed object while it runs, the object won't be garbage collected (a safe call is used that checks for any remaining references after the destructor is called). This allows you to implement destruction priorities yourself easily by using a central "DestructManager": function __destruct() { global $destructManager; $this->addObject($this, $priority); } function __real_destruct() { .... } Those will be collected in the destruction manager and when at shutdown, on the manager's own destructor you just need to sort the collection by priority and call the "real" destructor. Now, this will work fine, but has this downside that all objects will be held in the memory until script shutdown, so it's naturally not good for long running processes. Regards, Stan Vassilev

« previous php.internals (#41316) next »