...

View Full Version : PHP Class Loader



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!

MattF
05-09-2010, 03:33 PM
The return is totally pointless:



exit;
return;


The script has been killed by the exit. The return can never be called. By the way, if I remember correctly, (never used it myself, so may be incorrect), but could you not just the autoload function to achieve this functionality with far less code?

http://uk2.php.net/manual/en/function.spl-autoload.php

imkingdavid
05-09-2010, 06:41 PM
The return is totally pointless:



exit;
return;


The script has been killed by the exit. The return can never be called. By the way, if I remember correctly, (never used it myself, so may be incorrect), but could you not just the autoload function to achieve this functionality with far less code?

http://uk2.php.net/manual/en/function.spl-autoload.php

As for the exit-return bit, yeah I had noticed that but never thought to take it out. I'll just change it to return false instead of exit.

As for spl_autoload, I had not seen it before. I'll look into it, but I like having my own method so I know exactly how it works and everything.

MattF
05-09-2010, 06:52 PM
but I like having my own method so I know exactly how it works and everything.

Can't argue with that. I tend to be in that camp too. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum