note 80107 added to language.oop5.decon

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
When a script is in the process of die()ing, you can't count on the order in which __destruct() will be called.

For a script I have been working on, I wanted to do transparent low-level encryption of any outgoing data.  To accomplish this, I used a global singleton class configured like this:

class EncryptedComms
{
	private $C;
	private $objs = array();
	private static $_me;
	
	public static function destroyAfter(&$obj)
	{
		self::getInstance()->objs[] =& $obj;
		/*
			Hopefully by forcing a reference to another object to exist 
			inside this class, the referenced object will need to be destroyed
			before garbage collection can occur on this object.  This will force 
			this object's destruct method to be fired AFTER the destructors of
			all the objects referenced here.
		*/
	}
	public function __construct($key)
	{
			$this->C = new SimpleCrypt($key);
			ob_start(array($this,'getBuffer'));
	}
	public static function &getInstance($key=NULL)
	{
		if(!self::$_me && $key)
			self::$_me = new EncryptedComms($key);
		else
			return self::$_me;
	}
	
	public function __destruct()
	{
		ob_end_flush();
	}
	
	public function getBuffer($str)
	{
		return $this->C->encrypt($str);
	}

}

In this example, I tried to register other objects to always be destroyed just before this object.  Like this:

class A
{

public function __construct()
{
     EncryptedComms::destroyAfter($this);
}
}

One would think that the references to the objects contained in the singleton would be destroyed first, but this is not the case.  In fact, this won't work even if you reverse the paradigm and store a reference to EncryptedComms in every object you'd like to be destroyed before it.

In short, when a script die()s, there doesn't seem to be any way to predict the order in which the destructors will fire.
----
Server IP: 64.71.164.2
Probable Submitter: 74.78.225.168
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit        -- https://master.php.net/note/edit/80107
Del: integrated  -- https://master.php.net/note/delete/80107/integrated
Del: useless     -- https://master.php.net/note/delete/80107/useless
Del: bad code    -- https://master.php.net/note/delete/80107/bad+code
Del: spam        -- https://master.php.net/note/delete/80107/spam
Del: non-english -- https://master.php.net/note/delete/80107/non-english
Del: in docs     -- https://master.php.net/note/delete/80107/in+docs
Del: other reasons-- https://master.php.net/note/delete/80107
Reject      -- https://master.php.net/note/reject/80107
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.