我越來越深刻的PHP和我創建我自己的迷你mvc框架來學習OOP。php不能重新聲明_autoload()
我有一個.htaccess文件,將所有內容重定向到index.php。在index.php中,我包含一個名爲boootstrap.php的文件來解析url並加載類php文件。
現在我正在添加ActiveRecord http://www.phpactiverecord.org來添加數據庫訪問。我得到的錯誤:
致命錯誤:不能重新聲明類自動裝載在/home/i554246/public_html/mvc/lib/Autoloader.php第4行
我不知道如何停止衝突。
的index.php:其在bootstrap.php中包含
<?php
class AutoLoader
{
public static function Load($Class)
{
$File = BASEDIR.$Class.'.php';
if(is_readable($File))
{
require($File);
}
else
{
die('Requested module "'.$Class.'" is missing. Execution stopped.');
}
}
}
spl_autoload_register('AutoLoader::Load');
ActiveRecord.php
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_DISABLE'))
spl_autoload_register('activerecord_autoload',false,PHP_ACTIVERECORD_AUTOLOAD_PREPEND);
function activerecord_autoload($class_name)
{
$path = ActiveRecord\Config::instance()->get_model_directory();
$root = realpath(isset($path) ? $path : '.');
if (($namespaces = ActiveRecord\get_namespaces($class_name)))
{
$class_name = array_pop($namespaces);
$directories = array();
foreach ($namespaces as $directory)
$directories[] = $directory;
$root .= DIRECTORY_SEPARATOR . implode($directories, DIRECTORY_SEPARATOR);
}
$file = "$root/$class_name.php";
if (file_exists($file))
require $file;
}
?>
如果可能的話我不想改變什麼的ActiveRecord.php,因爲它是第三方,並可能導致升級問題。會有另一種方式嗎? –
也許,但我傾向於同意[Nanne](http://stackoverflow.com/users/546999/nanne)您的下一步應該是調試堆棧。我認爲你需要對事情發生順序的精確說明。同樣,對Nanne的命名空間的建議 - 即使這個問題的根源沒有命名空間衝突,額外的懲罰也會幫助你排除紅鯡魚。 –
是的,我會感謝 –