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
https://forums.phpfreaks.com/topic/227408-oop-static-vars-help/
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.

thats where my Autoloader_Exception class is gonna come in...but if that doesn't load (if file doesn't exsits) then i want the handler to be Exception...

 

is there a way of getting this to work?

catch (self::$_exception_handler $e)
{

}

 

thank you

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.