简体   繁体   中英

php cannot redeclare _autoload()

I getting more indepth with php and I am creating my own mini mvc framework to learn OOP.

I have a .htaccess file that redirects everything to the index.php. In the index.php I include a file called boootstrap.php to parse the url and load the class php file.

Now that I am adding ActiveRecord http://www.phpactiverecord.org to add database access. I get the error:

Fatal error: Cannot redeclare class AutoLoader in /home/i554246/public_html/mvc/lib/Autoloader.php on line 4

I am not sure how to stop the conflict.

index.php:

include(MVC_CORE_INCLUDE_PATH . DS . 'Bootstrap.php')
include(MVC_CORE_INCLUDE_PATH . DS . 'activerecord/ActiveRecord.php');

autoloader.php which is included in 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;
}
?>

Perhaps change

if (file_exists($file))
    require $file;

to

if (file_exists($file))
    require_once($file);

You either try to include a file twice (you might want to use require_once ) or you have 2 classes (one from a library you use?) with that same name.

if There seem to be 2 classes called AutoLoader , you might want to look into namespaces. I can't recall phpactiverecord having a class called that, but as you might use several libraries you were bound to run into this.

The best way would be to put your own autoloader class in a namespace . Make sure you keep all your calls correct, so calls to the autoloader should have \\yournamespace\\ in front of it, and calls inside the autoloader might need prepending a \\ to (like \\Exception for instance)

PHP-ActiveRecord doesn't have any AutoLoader class. What happens here I guess, is that you have two loaders that are loading the file.

Since it's PSR-0 compliant, you can load it using your own loading utility (assuming it's embracing that convention). If you do so just disable PHP-AR autoloading utility.

define('PHP_ACTIVERECORD_AUTOLOAD_DISABLE', true);

The vanilla loader is moslty useful for finding model classes which won't be needed if you're putting them in their own namespace. As your framework might not follow PHP-AR convention regarding where the models are, it seems correct to disable that autoloader.

Check that example of PHP-AR integration with lithium framework: li3_activerecord

I moved the line

$Bootstrap = new Bootstrap(); 

under the

include(MVC_CORE_INCLUDE_PATH . DS . 'Bootstrap.php'); 
include(MVC_CORE_INCLUDE_PATH . DS . 'Controller.php'); 

but above

require_once 'lib/activerecord/ActiveRecord.php'; the ActiveRecord seemed to want to load it again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM