Jump to content

Recommended Posts

hey guys i have a script ive made which loads classes automatically when called...the script works fine but when i extend the class Autoloader_Exception and it comes back with the error

 

Fatal error: Class 'Autoloader_Exception' not found in C:\www\library\autoloader.class.php on line 3

 

i might be missing something here but i dont know why the autoloader doesnt load the extended class

 

if someone can please help me how i can extend the class please thanks

 

<?php

class Autoloader extends Autoloader_Exception
{	
protected static $_declared_classes = array();

public static function load_class($class_name)
{	
    $class_name = ucwords($class_name);
	$file       = self::get_class_path($class_name);

	try
	{	
		if (!class_exists($class_name, FALSE))
		{
			if (file_exists($file))
			{
				require_once $file;
				self::$_declared_classes[] = $class_name;
			}
			else
			{
				throw new Exception(sprintf("Class '%s' not found.<br />\n", $class_name));
			}
		}
	}
	catch (Exception $e)
	{
		echo $e->getMessage();	
	}
}	

protected static function get_class_path($class_name)
{
	global $classes;

	if (array_key_exists($class_name, $classes))
	{
		return ROOT . DS . $classes[$class_name];
	}
}

public static function declared_classes()
{
	echo "<pre>";
	print_r(self::$_declared_classes);
	echo "</pre>";
}
}

spl_autoload_register(array('Autoloader', 'load_class'));

?>

Link to comment
https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/
Share on other sites

ok thanks for your help...but can you give me and example on where extending exceptions would be good within a class....a forms class, mysql class?

 

You're looking at it backwards.  You should extend Exception when you want to add functionality to Extension.  If you make a custom SQL_Exception class, it should extend Extension, not whatever class you're using to represent your database.  The database class can then throw your new SQL_Exception, which can be caught later down the line.

 

A database, a form, an autoloader... none of these are exceptions in and of themselves, nor should they be.  The most they should do is throw custom exceptions that relate specifically to them.

Just to be absolutely clear, something like (pseudo-code):

 

class Autoloader_Exception extends Exception{}

class Autoloader
{
   public someFunction()
   {
      if (/* some bad condition */)
      {
         throw new Autoloader_Exception();
      }
      else
      {
         // continue
      }
   }
}

 

Is just fine.

 

Also, Exception isn't an exception handler.  Exception simply encapsulates error data.  They're handled in try-catch blocks.

Nightslyr thanks of the example i think thats what i need...

 

just to use

 

instead of

 

throw new Autoloader_Exception

 

throw new Exception

 

Yeah, that would work.  However, just to be clear, it makes sense to subclass Exception if you need to track what kind of exception is being thrown, or if certain exceptions need more information to be recorded.

well thanks guys you've been very helpful...and ive finnished my autoloader class if you want to have a look

 

basically it has a function which will read all files in the directory ending with .class.php and store the path so it is ready to be called

 

<?php

class Autoloader
{	
private $_class_paths        = array();
private $_ignore_directories = array();
private $_declared_classes   = array();

public function __construct($config)
{
	$this->_ignore_directories = $config->autoloader_ignore_directories;
	$this->get_class_paths();

	spl_autoload_register(array($this, 'load_class'));
}

private function load_class($file)
{	
    $file  = strtolower($file);
	$path  = $this->_class_paths[$file];

	try
	{	
		if (!class_exists($file, FALSE))
		{
			if (file_exists($path))
			{
				require_once $path;
				$this->_declared_classes[] = $file;
			}
			else
			{
				throw new Autoloader_Exception(sprintf("Class '%s' not found.<br />\n", $file));
			}
		}
	}
	catch (Exception $e)
	{
		echo $e->getMessage();	
	}
}	

private function get_class_paths($path = ROOT)
{
    	$dh = opendir($path);

    while (false !== ($file = readdir($dh)))
	{    
        if (!in_array($file, $this->_ignore_directories))
        {
            if (is_dir($path . DS . $file))
            {
                $this->get_class_paths($path . DS . $file);
            } 
            else 
            {	
            	if (preg_match("/\.class\.php$/i", $file))
            	{
            		$file = substr($file, 0, -10);
            		$this->_class_paths[$file] = $path . DS . $file . '.class.php';
            	}
            }
        }
	}
    	
    closedir($dh);
}

public function declared_classes()
{
	echo "<pre>";
	print_r($this->_declared_classes);
	echo "</pre>";
}
}

 

if you guys can tell me what you think...or if you have any thing you think i should change please...thank you again

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.