09-06-2007, 01:07 AM
|
#1 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 438
Thanks: 22
|
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 02:44 PM.
Reason: I made a boo boo :(
|
|
|
|