douzhi3454 2011-09-02 14:40
浏览 34
已采纳

PHP5 AutoLoader SPL_AutoLoad失败

I have the code below as an autoload class, however it appears that the clean method simply isn't working and it always falls back on the dirty method.

Am I using spl_autoload incorrectly? If so what is the correct (better) way? Is this inefficient, how could it be improved?

I always get output such as the bottom when using this method though, and in some cases it just doesn't find the class but doesn't throw any error I have display errors set to 1 and have checked the error log but just completely missing.

The code gets initialised as

require "vendor/AutoLoader.class.php";
self::setGlobal("autoloader", AutoLoader::init());

And the class is as follows:

public static $instance;
private $_src=array('vendor/', 'lib/', '');
private $_sub=array('base/', '');
private $_ext=array('.php', 'class.php', 'lib.php');

/* initialize the autoloader class */
public static function init(){
    if(self::$instance==NULL){
        self::$instance=new self();
    }
    return self::$instance;
}

/* put the custom functions in the autoload register when the class is initialized */
private function __construct(){
    spl_autoload_register(array($this, 'clean'));
    spl_autoload_register(array($this, 'dirty'));
}

/* the clean method to autoload the class without any includes, works in most cases */
private function clean($class){
    $class=str_replace('_', '/', $class);
    spl_autoload_extensions(implode(',', $this->_ext));
    foreach($this->_src as $resource){
      foreach($this->_sub as $sub){
        echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
        set_include_path(pegFramework::getGlobal("baseDir") . $resource.$sub);
        spl_autoload($class);
        if(class_exists($class)) {
          echo 'Found and clean included '.$class.' in '.$resource.$sub."<br />";
          break 2;
        }
      }
    }
}

/* the dirty method to autoload the class after including the php file containing the class */
private function dirty($class){
    global $docroot;
    $class=str_replace('_', '/', $class);
    foreach($this->_src as $resource){
        foreach($this->_ext as $ext){
          foreach($this->_sub as $sub){
            echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
            if(@include(pegFramework::getGlobal("baseDir") . $resource . $sub. $class . $ext)) {
              echo 'Found and dirty included '.$class.' as '.$resource . $sub. $class . $ext."<br />";
              break 3;
            }
          }
        }
    }
    spl_autoload($class);
}

Trying to load pegDatabase via pegAutoloader::clean()
...snip...
Trying to load pegDatabase via pegAutoloader::clean()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load basepegDatabase via pegAutoloader::clean()
...snip...
Trying to load basepegDatabase via pegAutoloader::clean()
Trying to load basepegDatabase via pegAutoloader::dirty()
Found and dirty included basepegDatabase as vendor/base/basepegDatabase.php
Found and dirty included pegDatabase as vendor/pegDatabase.php
Trying to load pegRequest via pegAutoloader::clean()
...snip...
Trying to load pegRequest via pegAutoloader::clean()
Trying to load pegRequest via pegAutoloader::dirty()
Trying to load pegRequest via pegAutoloader::dirty()
Found and dirty included pegRequest as vendor/pegRequest.php
Trying to load pegFacebook via pegAutoloader::clean()
...snip...
Trying to load pegFacebook via pegAutoloader::clean()
Trying to load pegFacebook via pegAutoloader::dirty()
...snip...
Trying to load pegFacebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::clean()
Trying to load Facebook via pegAutoloader::clean()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
  • 写回答

1条回答 默认 最新

  • douhu2131 2011-09-12 03:06
    关注

    I think it's the call to spl_autoload that's tripping you up. In the comments of the php documentation, I found this:

    Note this function will LOWERCASE the class names its looking for, dont be confused when it cant find Foo_Bar.php

    So, calls to spl_autoload with pegFacebook or Facebook are just going to trigger searches for files named pegfacebook.php or facebook.class.php.

    If I were you, though, I would look at Symfony's UniversalClassLoader for examples of much more simplified handling of class loading. You could probably tweak it to use specific extensions, but I would at least think about finding a new way to handle this problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加