class IncludePath
{
private $pathToDirectory;
private $aPathToDirectory;
public function setPath($pathToDirectory = '')
{
self::setIncludePath(self::getArrayPathToDirectory($pathToDirectory));
spl_autoload_register(array('self', 'autoloadClass'));
}
private function getArrayPathToDirectory($pathToDirectory)
{
static $aPathToDirectory;
if (!isset($aPathToDirectory)) $aPathToDirectory = array();
if (is_dir($pathToDirectory)) {
$aNamesFileAndDirectory = scandir($pathToDirectory);
if (is_array($aNamesFileAndDirectory)) {
$aNamesFileAndDirectory = array_diff($aNamesFileAndDirectory, array('.', '..'));
if (!empty($aNamesFileAndDirectory)) {
foreach($aNamesFileAndDirectory as $nameFileOrDirectory) {
$fullPathToFileOrDirectory = $pathToDirectory.DIRECTORY_SEPARATOR.$nameFileOrDirectory;
if (is_dir($fullPathToFileOrDirectory)) {
$goodPath = explode($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR, $pathToDirectory.DIRECTORY_SEPARATOR.$nameFileOrDirectory.DIRECTORY_SEPARATOR);
$aPathToDirectory[] = $goodPath[1];
self::getArrayPathToDirectory($fullPathToFileOrDirectory);
}
}
}
}
}
return $aPathToDirectory;
}
private function setIncludePath($aPathToDirectory)
{
set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR, $aPathToDirectory));
}
private static function autoloadClass($className)
{
$fileName = strtolower($className.'.php');
require_once($fileName);
} }
IncludePath::setPath($_SERVER['DOCUMENT_ROOT']); # создать "дерево" каталогов относительно корневой директории $_SERVER['DOCUMENT_ROOT']
|
Теперь если вызвать метод MyFunction() класса MyClass мы просто напишем:
Класс автоматом погрузиться и не важно в какой директории будет находиться файл myclass.php с классом MyClass