I would suggest looking into spl_autoload
. just add the proper directories to your include_path
Something like this can help probably you get started:
ini_set($your_class_dir_here .PATH_SEPERATOR. ini_get('include_path'));
You'll have to either provide your own autoloader using spl_autoload_register
or lowercase all your filenames.
Here's one of my own autoloaders, which uses php namespaces to overcome some directory issues.
<?php
namespace Red
{
// since we don't have the Object yet as we load this file, this is the only place where this needs to be done.
require_once 'Object.php';
/**
* Loader implements a rudimentary autoloader stack.
*/
class Loader extends Object
{
/**
* @var Loader
*/
static protected $instance = null;
/**
* @var string
*/
protected $basePath;
/**
* @return Loader
*/
static public function instance()
{
if (self::$instance == null)
{
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize the autoloader. Future expansions to the
* autoloader stack should be registered in here.
*/
static public function Init()
{
spl_autoload_register(array(self::instance(), 'autoLoadInNamespace'));
}
/**
* PHP calls this method when a class is used that has not been
* defined yet.
*
* I'm returning a boolean for success which isn't required (php ignores it)
* but makes life easier when the stack grows.
*
* @param string $fullyQualifiedClassName
* @return boolean
*/
public function autoLoadInNamespace($fullyQualifiedClassName)
{
$pathParts = preg_split('/\\\\/', $fullyQualifiedClassName, -1, PREG_SPLIT_NO_EMPTY);
array_unshift($pathParts, $this->basePath);
$pathToFile = implode(DIRECTORY_SEPARATOR, $pathParts) . '.php';
if (file_exists($pathToFile))
{
require_once $pathToFile;
return true;
}
return false;
}
/**
* Constructor is protected because we don't want multiple instances
* But we do want an instance to talk to later on.
*/
protected function __construct()
{
$this->basePath = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..');
}
}
}
#EOF;
It's part of a class named Loader
in the \Red
namespace and gets initialized from a simple bootstrap file:
<?php
// This is where the magic is prepared.
require_once implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'Red', 'Loader.php'));
// Initialize the autoloader, no more require_once for every class
// and everything is OOP from here on in.
Red\Loader::Init();
#EOF