Jump to content

Scope Of __destruct Method


MrAdam
 Share

Recommended Posts

Hi guys.I've been writing a little class to handle my config functionality but gotten a little stuck with the destruct method. Exactly what scope does the destruct method have? In my class it can't seem to recognize any of the class vars or methods that I've created...ThanksAdam

Link to comment
Share on other sites

The destructor generally runs like you would expect, with the possible exception that you can't guarantee the order in which multiple objects get destroyed. It runs in the normal scope, and should have access to everything the other methods have access to. If your class is using properties that are objects from another class, they might be getting destroyed before your other class. If you need to guarantee that a certain object is still available when the destructor gets called, you can keep a reference to that object in the class. It won't destroy an object unless there are no references to it. I do that with a config option class I made to guarantee that it still has access to the database object when the destructor gets called:

<?phpclass tc_lms_config{  private $fields;  private $dirty;  private $db;  private $debug;  private $fpath;  private $db_keys;  function __construct()  {	global $db;	/*	Why it is necessary to keep a reference to global db:	  This class uses the db object in the destructor.  There is no guarantee	  which order objects will get destroyed in.  An object will not get destroyed	  if there are references to it somewhere else.  Storing a reference to the	  global db object in this class ensures that this object will be available	  for the destructor to use.	  A side-effect of this is that this class requires the db object to have	  already been created when an object of this class is instantiated.	*/	$this->debug = false;	$this->fpath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.log';	$this->fields = array();	$this->dirty = array();	$this->db_keys = array();	$this->db = $db;	if ($this->debug)	  file_put_contents($this->fpath, date('r') . " Config class created\n", FILE_APPEND);  }  function __destruct()  {	# commit changes	$tmp = array();	foreach ($this->dirty as $k => $v)	{	  $tmp[] = $k;	  $update = array('val' => $this->fields[$k]);	  if (in_array($k, $this->db_keys))		$this->db->update('config', $update, "`name`='{$k}'");	  else		$this->db->insert('config', array('name' => $k, 'val' => $this->fields[$k]));	}	if ($this->debug)	  file_put_contents($this->fpath, date('r') . " Config class destroyed.  Updates:\n" . print_r($tmp, true), FILE_APPEND);  }  function get_opt($id)  {	if ($this->debug)	  file_put_contents($this->fpath, date('r') . " Get option ({$id})\n", FILE_APPEND);	if (!count($this->fields))	  $this->load_opts();	if (isset($this->fields[$id]))	{	  if ($this->debug)		file_put_contents($this->fpath, date('r') . " Value: {$this->fields[$id]}\n", FILE_APPEND);	  return $this->fields[$id];	}	if ($this->debug)	  file_put_contents($this->fpath, date('r') . " Option not found\n", FILE_APPEND);	return false;  }  function load_opts()  {	if ($this->debug)	  file_put_contents($this->fpath, date('r') . " Load options\n", FILE_APPEND);	$this->db->sql('SELECT * FROM config ORDER BY `name` ASC');	$results = $this->db->select();	for ($i = 0; $i < count($results); $i++)	{	  $this->fields[$results[$i]['name']] = $results[$i]['val'];	  $this->db_keys[] = $results[$i]['name'];	}	if ($this->debug)	  file_put_contents($this->fpath, date('r') . " Options loaded:\n" . print_r($this->fields, true), FILE_APPEND);  }  function set_opt($id, $val)  {	if ($this->debug)	  file_put_contents($this->fpath, date('r') . " Set option ({$id}, {$val})\n", FILE_APPEND);	if (!count($this->fields))	  $this->load_opts();	$this->fields[$id] = $val;	$this->dirty[$id] = true;  }}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...