Here’s a autoload that I use:
function search_lib($lib, $file, $ds = '/'){
// Verifica se o diretório informado é válido
if (is_dir($lib)){
// Verifica se o arquivo já existe neste primeiro diretório
if (file_exists($lib.$ds.$file)) return $lib.$ds.$file;
// Lista os subdiretórios e arquivos
$dirs = array_diff(scandir($lib, 1), array('.','..'));
foreach ($dirs as $dir) {
// Verifica se é um arquivo se for, pula para o próximo
if (!is_dir($lib.$ds.$dir)) continue;
// Se for um diretório procura dentro dele
$f = search_lib($lib.$ds.$dir, $file, $ds);
// Caso não encontre retora false
if ($f !== false) return $f;
}
}
// Se o diretório informado não for válido ou se não tiver encontrado retorna false
return false;
}
spl_autoload_register(
function ($class){
//if (strpos($class, '\\') !=- -1)
// $class = end( explode('\\', $class) );
$libs = __DIR__.DS;
$ext = '.class.php';
$file = search_lib($libs, $class.$ext);
// Se encontrou inclui o arquivo
if ($file !== false ) require_once $file;
// Se não encontrar o arquivo lança um erro na tela. :)
else {
$msg = "Autoload fatal erro: Can't find the file {$class}{$ext} on {$libs}!";
error_log($msg);
exit('<br><br><strong>'.$msg.'</strong>');
}
}
);
It searches the class within the directory (and its subdirectories) in the variable $libs
of the anonymity function in the spl_autoload_register
. The extension of class files is configured in the variable $ext
just below (I use .class.php
, just change to your default).
Obs.: The code uses a constant DS
which is an alias that I created for DIRECTORY_SEPARATOR
, if you don’t use it so just change, or if you want to set it.
Could you translate the question?
– rray
Fuck!! Sorry I didn’t even notice kkkkk pera ae....
– João Paulo Vieira da Silva
See help: http://devcia.com/criando-um-autoload-turbined/
– KaduAmaral