doucanrui1735 2011-03-08 18:58
浏览 15
已采纳

自动加载类

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..

  • 写回答

1条回答 默认 最新

  • duanmu6752 2011-03-08 20:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析