现在用的是autoload(代码如下:),还有别的方法能自动加载类文件吗?除了include(具体文件路径)function __autoload($className){
if(file_exists('model/'.$className.'.class.php')){
include_once('model/'.$className.'.class.php');
}
if(file_exists('Controller/'.$className.'.class.php')){
include_once('Controller/'.$className.'.class.php');
}$prefix=$GLOBALS['groupName']=='main'?'':'../';
if(file_exists($prefix.'core/'.$className.'.class.php')){
include_once($prefix.'core/'.$className.'.class.php');
}
if(file_exists($prefix.'core/Smarty-3.1.6/'.$className.'.class.php')){
include_once($prefix.'core/Smarty-3.1.6/'.$className.'.class.php');
}
if(file_exists($prefix.'core/Smarty-3.1.6/sysplugins/'.$className.'.php')){
include_once($prefix.'core/Smarty-3.1.6/sysplugins/'.$className.'.php');
}}

解决方案 »

  1.   

    function __autoload(){}这是一个自动加载类的方法。也就是说,在你实例化一个对象时,如果没有这个类,会自动执行__autoload方法,然后加载过来,而引入文件除了include,include_once外,还有require,require_once,这两个有点本质上的区别,具体的找百度,google搜一下就知道了!
      

  2.   

    不懂你的意思....说到底最后都是include
      

  3.   

    这个还是可以优化的,用set_include_path()把你要的目录包含进来, 这样就不用去判断你的文件目录了,不过这样的方法只适合于没有重复的文件命名的情况下使用.
      

  4.   

    你是不是嫌这比较麻烦?
    其实是因为你类文件放的地方比较多,而且你上面应该是用if else if或switch...case。语句。
    如果你嫌麻烦的话可以用个数组来保存所有的路径比如。
    $arr = array('classPath1','classPath2','classPath3');
    foreach($arr as $v){
      $new_path = $v.$className.'.php';
      if(file_exists($new_path)){
        include_once($new_path);
        break;
      }
    }
    这样就比较方便点,
      

  5.   

    第一: 没有什么影响,或者说影响可以忽略, 你可以自己测试.第二: 类名应该反映其用途
    比如你这几句: 
    if(file_exists('model/'.$className.'.class.php')){
    include_once('model/'.$className.'.class.php');
    }
    if(file_exists('Controller/'.$className.'.class.php')){
    include_once('Controller/'.$className.'.class.php');
    }
    从$className应该就能看出是controller还是model,应该不需要一个一个目录找,
    而且,至少可以加个else吧
      

  6.   

    哦,有个地方应该理解错了,
    可能你是需要很多个class,所以用IF就可以了,代码中的break不要。
    或者file_exists也不要,@include_once(屏蔽错误信息)这样应该比较快。
      

  7.   

    break可以要, 他不可能载入多个同名的类.