[问答题] 用autoload($class) 实现Lib目录下的类的自动加载并可以兼容子目录

$request->action = lcfirst(implode(array_map(
'ucfirst',
explode('-', strtolower($request->action))
)));
------------------------------------------------------------
function __autoload($class)
{
$cls = strtolower(str_replace("_","/",$class));
if(file_exsits(LIB.$cls.'.php'))
{
include_once(LIB.$cls.'.php');
}
else
{
die("not found {$class} class");
}
}
defined("LIB",'/data/wwwroot/www.xx.com/lib/');
$author = new Lib_Author();
-----------------------------------------------------------
function __authload($class)
{
$cls = explode("_",$class);
if(@is_dir($cls[1]))
{
if(@is_file($cls[2]))
{
include_once("CON_PATH".$cls[1].'/'.$cls[2].".php");
}
else
{
dir('error');
}
}
else if(@is_file($cls[1].".php"))
{
include_once("CON_PATH".$cls[1].".php");
}
else
{
dir('error');
}
}
---------------------------------------
function __autoload($class)
{
$cls = explode("_",$class);
$file = get_file($cls);
if($file=='error')
{
die('error');
}
include_once($file);
}
function get_file($dir)
{
if(is_array($dir))
{
foreach($dir as $k=>$v)
{
$tmpdir .= $v.'/';
if(is_dir('CON_PATH'.$tmpdir))
{
continue();
}
else if(is_file('CON_PATH'.$tmpdir.".php"))
{
return 'CON_PATH'.$tmpdir.".php";
}
else
{
return 'error';
}
}
return 'error';
}
return 'error';
}
defined("CON_PATH","/data/wwwroot/www.xx.com/app/cntroller/");
$sb = new controller_sb();
------------------------------------
function __autoload_my_classes($classname)
{
# ... your logic to include classes here
}
spl_autoload_register('__autoload_my_classes');
-----------------------------------------------------------

转载于:https://www.cnblogs.com/pizishui/p/5349975.html

你可能感兴趣的:([问答题] 用autoload($class) 实现Lib目录下的类的自动加载并可以兼容子目录)