我对php有了更深入的了解,我正在创建自己的迷你mvc框架来学习OOP。
我有一个 .htaccess 文件,可以将所有内容重定向到索引.php。在索引中.php我包含一个名为 boootstrap 的文件.php用于解析 url 并加载类 php 文件。
现在,我正在添加活动记录 http://www.phpactiverecord.org 以添加数据库访问权限。我收到错误:
致命错误:无法在第 4 行的/home/i554246/public_html/mvc/lib/Autoloader.php 中重新声明类 AutoLoader
我不知道如何制止冲突。
索引.php:
include(MVC_CORE_INCLUDE_PATH . DS . 'Bootstrap.php')
include(MVC_CORE_INCLUDE_PATH . DS . 'activerecord/ActiveRecord.php');
自动加载机.php包含在引导程序中.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');
活动记录.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;
}
?>
也许会改变
if (file_exists($file))
require $file;
自
if (file_exists($file))
require_once($file);
您要么尝试包含两次文件(您可能想使用 require_once
),要么有 2 个类(一个来自您使用的库?)具有相同的名称。
如果似乎有 2 个类称为 AutoLoader
,您可能需要查看命名空间。我不记得phpactiverecord有一个叫做这个的类,但是由于你可能使用几个库,你一定会遇到这个。
最好的方法是将您自己的自动加载器类放在namespace
中。确保所有调用都正确,因此对自动加载器的调用前面应有'yournamespace'
,并且自动加载器内的调用可能需要在前面加上'
(例如'Exception
)
PHP-ActiveRecord 没有任何AutoLoader
类。我猜这里发生的事情是,您有两个加载器正在加载文件。
由于它符合 PSR-0 标准,因此您可以使用自己的加载实用程序加载它(假设它采用该约定)。如果这样做,只需禁用PHP-AR自动加载实用程序。
define('PHP_ACTIVERECORD_AUTOLOAD_DISABLE', true);
vanilla loader 对于查找模型类非常有用,如果您将它们放在自己的命名空间中,则不需要这些模型类。由于您的框架可能不遵循有关模型位置的 PHP-AR 约定,因此禁用该自动加载器似乎是正确的。
查看PHP-AR与锂框架集成的示例:li3_activerecord
我移动了行
$Bootstrap = new Bootstrap();
在
include(MVC_CORE_INCLUDE_PATH . DS . 'Bootstrap.php');
include(MVC_CORE_INCLUDE_PATH . DS . 'Controller.php');
但以上
require_once 'lib/activerecord/ActiveRecord.php'; the ActiveRecord seemed to want to load it again