2012-11-30 42 views
0

自動加載類有一個神奇的功能(__autoload),我想知道是否有一種方法來加載沒有類的文件。自動加載文件,當使用需要功能

事情是這樣的:

require ('example_file'); // Trigger __autoloadfiles function 

function __autoloadfiles($filename) 
{ 
    $files = array(
     ROOT . DS . 'library' . DS . $filename. '.php', 
     ROOT . DS . 'application' . DS . $filename . '.php', 
     ROOT . DS . 'application/otherfolder' . DS . $filename. '.php' 
    ); 

    $file_exists = FALSE; 

    foreach($files as $file) { 
     if(file_exists($file)) { 
      require_once $file; 
      $file_exists = TRUE; 
      break; 
     } 
    } 

    if(!$file_exists) 
     die("File not found."); 

} 

回答

1

您可以定義自己的功能要求:

function require_file($file) { 
    // your code 
} 

然後調用它

require_file('file'); 

我想這是沒有辦法過載require功能。