imkingdavid
05-09-2010, 01:29 PM
Here's a handy little function I created to load classes. I'll provide the actual function and then an example of how to use it with a class. You're welcome to use this, but please leave the copyright intact/give credit where it's due!
/*
load - Loads the selected class file, initializes the class.
Parameters
$class = PHP class file
NOTE: Do NOT include .php
ALSO NOTE: The class name should be the file name.
$mode = The mode to run; this allows the same class file to be used for multiple pages if needed.
(Optional) Default: main
NOTE: all classes must have a main mode to default to!
Return - Returns the class in variable $class
*/
function load($class, $mode = 'main')
{
$class_file = ROOT_PATH . 'path/to/classes/
' . $class . PHP_EXT;
//! File does not exist; we end the script with a nice little error message
if(!file_exists($class_file))
{
return false;
}
else //! File exists, so we move on.
{
include($class_file); //! Include the file
//! See if class exists in the included class file.
//! Because it should be the same as the file name, we can just use the class file name provided.
if(!class_exists($class)) //! If it does not exist, show a nice little error message and abort.
{
return false;
}
else //! Class exists, make an object using a variable variable. :D lol
{
$class = new $class;
$class->main($mode);
return $class; //! Return the class object
}
}
}
Here's an example of a class to use it with:
class test
{
/*
main - Initialize the class for the current page
Parameters
$mode = The mode to use; (Optional) Default: main
*/
function main($mode = 'main')
{
//! All methods should have a main $mode option available, as that is
//! the default value given if no mode is specified.
switch($mode)
{
default:
case 'main':
//default content
break;
//! All classes should have a silent mode
case 'silent':
//! Nothing here...
// this way, you can call the class but not output anything; so you can access methods and class variables in other parts of the site.
break;
}
}
}
And here's how it's used in your index file (note: with this method, only an index file is needed, other pages are accessed with ?i=<page>).
//! Let's get it started in here...
//! $i is the module to load
$i = $_GET['i'];
//! $mode is the mode to run
$mode = $_GET['mode']
switch($i)
{
case 'index':
case 'main':
case 'site':
$main = load('test', $mode);
break;
default:
$main = load($i, $mode);
break;
}
//! Assuming you have put all the stuff you need into the class mode that you are using, nothing else needs to go below this line.
//! Cheers!
/*
load - Loads the selected class file, initializes the class.
Parameters
$class = PHP class file
NOTE: Do NOT include .php
ALSO NOTE: The class name should be the file name.
$mode = The mode to run; this allows the same class file to be used for multiple pages if needed.
(Optional) Default: main
NOTE: all classes must have a main mode to default to!
Return - Returns the class in variable $class
*/
function load($class, $mode = 'main')
{
$class_file = ROOT_PATH . 'path/to/classes/
' . $class . PHP_EXT;
//! File does not exist; we end the script with a nice little error message
if(!file_exists($class_file))
{
return false;
}
else //! File exists, so we move on.
{
include($class_file); //! Include the file
//! See if class exists in the included class file.
//! Because it should be the same as the file name, we can just use the class file name provided.
if(!class_exists($class)) //! If it does not exist, show a nice little error message and abort.
{
return false;
}
else //! Class exists, make an object using a variable variable. :D lol
{
$class = new $class;
$class->main($mode);
return $class; //! Return the class object
}
}
}
Here's an example of a class to use it with:
class test
{
/*
main - Initialize the class for the current page
Parameters
$mode = The mode to use; (Optional) Default: main
*/
function main($mode = 'main')
{
//! All methods should have a main $mode option available, as that is
//! the default value given if no mode is specified.
switch($mode)
{
default:
case 'main':
//default content
break;
//! All classes should have a silent mode
case 'silent':
//! Nothing here...
// this way, you can call the class but not output anything; so you can access methods and class variables in other parts of the site.
break;
}
}
}
And here's how it's used in your index file (note: with this method, only an index file is needed, other pages are accessed with ?i=<page>).
//! Let's get it started in here...
//! $i is the module to load
$i = $_GET['i'];
//! $mode is the mode to run
$mode = $_GET['mode']
switch($i)
{
case 'index':
case 'main':
case 'site':
$main = load('test', $mode);
break;
default:
$main = load($i, $mode);
break;
}
//! Assuming you have put all the stuff you need into the class mode that you are using, nothing else needs to go below this line.
//! Cheers!