zoukankan      html  css  js  c++  java
  • [问答题] 用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');
    -----------------------------------------------------------

  • 相关阅读:
    shell基础--变量的数值计算
    shell基础--字符串和变量的操作
    shell基础--shell特殊变量
    Asp.net MVC 控制器扩展方法实现jsonp
    ASP.NET web api 跨域请求
    asp.net Web API简单的特性路由配置
    ASP.Net Web API 输出缓存 转载 -- Output caching in ASP.NET Web API
    基础拾遗 C# Json 与对象相互转换
    内存溢出和内存泄漏
    软件测试面试题(一)
  • 原文地址:https://www.cnblogs.com/pizishui/p/5349975.html
Copyright © 2011-2022 走看看