Home > Lab > Class Loader

Class Loader

I needed a class loader for my little framework as I dont want to include all the files in on every request which is what listing them in a long list of includes will do. So I checked out the PHP.net site which has this code sample. I used it as a starting point for this:

function __load_class($classname, $dir)
{
	$file = $dir . '/' . $classname . '.php';
	if (file_exists($file))
	{
		require_once ($file);
		return true;
	}
	return false;
}
 
function __autoload($classname)
{
	$inc[] = '../fwork';
	$inc[] = '../fwork/controllers';
	$inc[] = '../fwork/models';
	$inc[] = '../app';
	$inc[] = '../app/controllers';
	$inc[] = '../app/models';
 
	foreach ($inc as $dir)
	{
		if (__load_class($classname, $dir))
		{
			if(DEBUG_LEVEL == 2)
			{
				echo 'Loading class(' . $classname . ")<br>";
			}
			return;
		}
	}
}

I need to do some more testing and abstraction but its a good start for what I need.

Leave a Reply

Trackbacks:0

Listed below are links to weblogs that reference
Class Loader from rhythmicalmedia.com
TOP