Jump to content

oop static vars help


Destramic

Recommended Posts

hey guys i have a static attribute $_exception_handler which is set to Exception and....now this value could change to a customer exception_handler but what i want to do is

 

<?php

catch (self::$_exception_handler $e)
{

}

 

but im getting an error...is there a way of doing this...any help would be greatful thanks

 

class below

<?php

class Autoloader
{	
protected static $_exception_handler = Exception;
protected static $_classes           = array();

public static function load_library($class_name)
{	
	$file = ROOT . DS. LIBRARY_DIRECTORY . DS . $class_name . CLASS_EXTENSION;

	try
	{
		self::load_class($class_name, $file);
	}
	catch (self::$_exception_handler $e)
	{
		echo $e->getMessage();
	}
}

public static function load_exception($class_name)
{

}

public static function load_class($class_name, $file)
{
	if (!class_exists($class_name, FALSE))
	{
		if (file_exists($file))
		{
			require_once $file;
		}
		else
		{
			throw new Exception(sprintf('Class %s not found.', $class_name));
		}
	}
}	
}

Link to comment
Share on other sites

Why not make a custom Exception object which inherits from Exception?  Instead of storing a custom Exception within your static class (which isn't doing what you hope to do), simply throw your custom Exception when necessary and catch it like normal.

 

In other words:

 

Custom_Exception extends Exception
{
   // custom properties and methods
}

class Autoloader
{
   protected static $classes = array();

   public static function load_library($class_name)
   {
      // ...

      try
      {
         self::load_class($class_name, $file);
      }
      catch(Custom_Exception $e)
      {
         echo $e->message;
      }
   }
}

 

This will allow you to catch various kinds of Exceptions, so if something doesn't fit your custom criteria, you could still handle more generic errors.

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.