Stephen Philbin
02-09-2005, 08:40 PM
Hi folks. If like me you almost resorted to going back to using long lists of includes for your classes because the handy new __autoload() seemed a bit sluggish and unwieldy, I have just the thing for you. :D
I've just put together a little bit of a script you can chuck into a file wich will handle all of your class files for you. ;) If you have a config or options file (or any kind of file that is included on every page before the headers are sent you can just chuck this script right in it and let it handle all your classes for you. :eek:
here's the scriptlet:
function __autoload($class)
{
$path = "includes/"; /* Path from server root to folder that
holds all of your class files. */
$euriai = -2; #Just leave this one.
$euriar = explode("/",$_SERVER['REQUEST_URI']); #And this one.
foreach($euriar as $junk_variable)
{++$euriai;}
if($euriai < 1)
{$reachback = NULL;}
elseif($euriai > 0)
{
$rbi = 0;
$reachback = "";
while($rbi < $euriai)
{$reachback .= "../";++$rbi;}
}
require_once($reachback.$path.$class.".php");
}
All you need to do is set $path to the path from the root of your http server to the directory containing all of your files for class descriptions. Make sure you include the directory name and the foward slash after it, but don't add anything after the slash (like a file name for example).
So long as the file containing the fuction is included before the headers are sent and the class you need is in a file in the specified directory you'll never need to worry about your classes for the rest of your project. :D
Hope you find it useful folks! ;)
*Wanders off feeling all warm and fuzzy inside*
Oh I almost forgot. Make sure the file name of the class definition is the same as the class definition it contains. So main_class.php contains:
class main_class {
stuff to define class;
}
and extending_class.php contains:
class extending_class extends main_class {
more class defining gubbins;
}
Enjoy! :D
I've just put together a little bit of a script you can chuck into a file wich will handle all of your class files for you. ;) If you have a config or options file (or any kind of file that is included on every page before the headers are sent you can just chuck this script right in it and let it handle all your classes for you. :eek:
here's the scriptlet:
function __autoload($class)
{
$path = "includes/"; /* Path from server root to folder that
holds all of your class files. */
$euriai = -2; #Just leave this one.
$euriar = explode("/",$_SERVER['REQUEST_URI']); #And this one.
foreach($euriar as $junk_variable)
{++$euriai;}
if($euriai < 1)
{$reachback = NULL;}
elseif($euriai > 0)
{
$rbi = 0;
$reachback = "";
while($rbi < $euriai)
{$reachback .= "../";++$rbi;}
}
require_once($reachback.$path.$class.".php");
}
All you need to do is set $path to the path from the root of your http server to the directory containing all of your files for class descriptions. Make sure you include the directory name and the foward slash after it, but don't add anything after the slash (like a file name for example).
So long as the file containing the fuction is included before the headers are sent and the class you need is in a file in the specified directory you'll never need to worry about your classes for the rest of your project. :D
Hope you find it useful folks! ;)
*Wanders off feeling all warm and fuzzy inside*
Oh I almost forgot. Make sure the file name of the class definition is the same as the class definition it contains. So main_class.php contains:
class main_class {
stuff to define class;
}
and extending_class.php contains:
class extending_class extends main_class {
more class defining gubbins;
}
Enjoy! :D