我目前正在用PHP创建我的第一个网站。我不想为每个单独的页面编写自动加载,而是希望创建一个具有一般自动加载功能的文件。spl_autoload可以放在另一个文件中吗?
这里是我的autoloadControl.php
:
// nullify any existing autoloads
spl_autoload_register(null,false);
//specify extensions that may be loaded
spl_autoload_extensions('.php. .class.php');
function regularLoader($className){
$file = $className.'.php';
include $file;
}
//register the loader function
spl_autoload_register('regularLoader');
这里是我的index.php
文件:
require("header.php");
require("autoloadControl.php");
$dbConnection = new dbControl();
$row=$dbConnection->getLatestEntry();
目前,$dbConnection = new dbControl()
给了我以下错误:
Fatal error: Class 'dbControl'
所以我的问题是,有没有办法使用自动加载这种方式,或者我必须放置它在我写的使用另一个文件的每个PHP文件的顶部?
你可以将它放在另一个文件中,这是完全可以的,因为你需要这个文件。唯一重要的是它被执行,这就是你的代码的情况。但是你有一个配置问题。哪个文件名有'dbControl'类,哪个目录放置了所有这些文件? – hakre 2012-08-08 16:32:39
你能粘贴完整的错误吗? – Florent 2012-08-08 16:34:45