dongwo5686 2011-06-09 09:39
浏览 87
已采纳

在包含包含它的文件后,PHP不会实例化该类

Here is my problem. I have one file (say, func.inc.php) with autoloader function, registered by spl_autoload_register():

function autoloaderFnc($class) {
    global $__CONFIG;

    $original = $class_name;
    $class_name = mb_strtolower ( $class_name );

    foreach ( $__CONFIG ['include_dirs'] as $path ) {
        if(file_exists ( $__CONFIG ['root'] . $path . $class_name . '.inc.php' )) {
            require ($__CONFIG ['root'] . $path . $class_name . '.inc.php');

            $classFound = TRUE;
            $foundAt = $__CONFIG ['root'] . $path . $class_name . '.inc.php';
            break;
        }
    }

    if( $classFound ) {
        if ( class_exists( $original ) ) {
            return true;
        } else {
            $backtrace = debug_backtrace();
            $error = "PHP User error: Cannot load class <b>$original</b> included at " . $backtrace[1]['file'] . ":" . $backtrace[1]['line'] . " (searching for <i>$class_name.inc.php</i>), but following file was included: $foundAt";
            error_log( $error );

            return false;
        }
    } else {
        $backtrace = debug_backtrace();
        $error = "PHP User error: Cannot find file with class <b>$original</b> included at " . $backtrace[1]['file'] . ":" . $backtrace[1]['line'] . " (searching for <i>$class_name.model.php</i> and <i>$class_name.inc.php</i>) in none of the following include dirs:<br />" . join ( '<br />', $__CONFIG ['include_dirs'] );
        error_log( $error );
    }

}

spl_autoload_register('autoloaderFnc');

Then I have a second file (show_me_poi.php), calling some class:

POI::doSmth();

Everything seems OK, but sometimes (and only sometimes!) I receive a following error in log:

[error] PHP User error: Cannot load class POI included at /path_to_dir/php/generators/export.generator.php:156 (searching for poi.inc.php), but following file was included: /path_to_dir/php/scripts/php/incs/poi.inc.php

This is weird, because class POI is defined in properly included file! And I repeat, this situation happens only sometime (10 out of 100 I think). What can cause such behaviour? And how can I fix it?

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • doujiao2000 2011-07-06 14:20
    关注

    The problem seems to arise only with APC op-cache turned on. I think that __autoload function can't work properly with caches in some cases. AFAIS only some PHP and APC versions are not compatible with each other.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败