2011-03-29 30 views
0

我有一個自動加載功能如下:包括智者在PHP的__autoload給人類「Smarty_Internal_Template」未找到

function __autoload($class) 
{ 
    //define('DOCROOT', dirname(__FILE__)); 

    $filename = "../sys/class/class." . strtolower($class) . ".inc.php"; 
    //$filename = DOCROOT . "/sys/class/class." . strtolower($class) . ".inc.php"; 

    if (file_exists($filename)) 
    { 
      include_once $filename; 
    } 

} 

我改名Smarty的文件class.smarty.inc.php所以包括它在自動加載,但我得到這個錯誤:

Fatal error: Class 'Smarty_Internal_Template' not found in /var/www/v3/sys/class/class.smarty.inc.php on line 441 

不知道這是什麼意思..

+0

什麼版本的Smarty? – 2011-03-29 17:27:27

+0

您應該總是使用'spl_autoload_register()',因爲如果其他庫在其中註冊自動加載器,'__autoload()'不會再被調用('spl_autoload_register('__ autoload');') – KingCrunch 2011-03-29 17:51:19

回答

2

不是修改第三方庫。只需創建一個遵循Smarty命名約定的第二個自動加載器。

function defaultAutoloader($className) { 
    // your code ($file = /path/to/my/lib/{{ CLASS }}.inc.php) 

    if (file_exists($file)) { 
     require $file; 
     return true; 
    } 

    return false; 
} 

function smartyAutoloader($className) { 
    // code ($file = /path/to/smarty/{{ CLASS }}.php) 

    if (file_exists($file)) { 
     require $file; 
     return true; 
    } 

    return false; 
} 

spl_autoload_register('defaultAutoloader'); 
spl_autoload_register('smartyAutoloader'); 
+0

不能說我同意你的第一個聲明,在我的書中修改第三方庫沒什麼問題,但它現在起作用了。謝謝 – ganjan 2011-03-29 19:24:21

0

磁帶自動加載機的類名映射到一個文件名的方式導致文件名class.smarty_internal_template.inc.php,這顯然不是你期望的文件名。我不知道Smarty是如何構建的,但是你應該確保自動加載器能夠找到它的任何類。