Bv202
Bv202

Reputation: 4044

Autoloading classes

I have created a Database class named Database.class.php in my includes folder. This is my code:

require_once 'includes/smarty/Smarty.class.php';
require_once 'includes/admin.functions.php';

function __autoload($class) {
    require_once 'includes/'.$class.'.class.php';
}

try {
    $db = Database::getInstance(); 
} catch (PDOException $e) { die('Error connecting to database.'); }

Is there anything wrong with this code? The first 2 includes works fine, I'm only getting a "class Database not found" error.

Does autoload only work when creating new object? If so, is there a workaround for singletons? Or am I doing something wrong?

Thanks!

EDIT: When manually including, it works. So it's really related to the autoloading..

Upvotes: 0

Views: 1289

Answers (1)

rojoca
rojoca

Reputation: 11190

Smarty registers an autoload function with spl_autoload_register. If you have your own autoload function you must register it with spl_autoload_register too if you want them to work together.

Upvotes: 2

Related Questions