我們以前只包含存儲在{project_root}/includes
文件夾中的類。我們使用自動加載功能在我們的應用程序中包含我們需要的類。現在,我想用一些圖書館,我面臨着一個問題:PHP無法自動載入類
1)自動加載:
// {project_root}/includes/autoLoad.php
// There is a global_connfig.php file that loads by directive in php.ini
// auto_prepend_file = /var/www/global_config.php which includes autoload.php file and sets the include path to {project_root}/includes
function __autoload($classname){
include "$classname.php";
}
2)的代碼,我想用:
//just an example from the monolog reference
// I put Monolog folder with it's subfolders in {project_root}/includes
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger("name");
$log->pushHandler(new StreamHandler(LOGSPATH . '/monolog', Logger::WARNING));
$log->warning('Foo');
$log->error('Bar');
3)錯誤:
Warning: include(Monolog\Logger.php): failed to open stream: No such file or
directory in {project_root}/includes/autoLoad.php
我試着用這樣的東西:autoloading classes in subfolders,但仍然得到 Class 'Monolog\Logger' not found
問題更新
哪裏是你的自動加載功能註冊?您是否嘗試過「include _ _ DIR _ _。」/ $ classname.php「;? – Thibault
在您要使用的代碼中,哪裏包含includes/autoLoad.php? – Thibault
您是否有Monolog \ Logger類的某處在你的include_path中? – Thibault