View Single Post
Old 09-06-2007, 12:07 AM   #1 (permalink)
Karl
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default Tip: Autoloading class files

You can autoload class files using the __autoload() function. Simply define the function with a single argument (this will be the name of the class) .

Code:
function __autoload($szClassName)
{
		$szFilePath = "lib/$szClassName.php";
		
		if (file_exists($szFilePath))
		{
			include_once($szFilePath);
		}
}
Now if you try to instantiate a class that is unknown, PHP will first try to load the class file via the code we setup in the __autoload funciton before throwing a fatal error.

Last edited by Karl : 09-06-2007 at 01:44 PM. Reason: I made a boo boo :(
Karl is offline  
Reply With Quote