简体   繁体   中英

Smarty and NetBeans autoload conflict

I have started learning NetBeans and tried to implement Smarty template engine in their To-Do example. When i try to run app, i get error: "Class "Smarty_Internal_TemplateCompilerBase" not found." I have found out that this is autoload conflict, since both my and smarty code use that. Here is my code:

 spl_autoload_register(array($this, 'loadClass'));

    public function loadClass($name) {
    $classes = array(
        'Config' => '../config/Config.php',
        'Error' => '../validation/Error.php',
        'Flash' => '../flash/Flash.php',
        'NotFoundException' => '../exception/NotFoundException.php',
        'TodoDao' => '../dao/TodoDao.php',
        'TodoMapper' => '../mapping/TodoMapper.php',
        'Todo' => '../model/Todo.php',
        'TodoSearchCriteria' => '../dao/TodoSearchCriteria.php',
        'TodoValidator' => '../validation/TodoValidator.php',
        'Utils' => '../util/Utils.php',
        'Smarty'=> '../smarty/libs/Smarty.class.php',
    );
    if (!array_key_exists($name, $classes)) {
        die('Class "' . $name . '" not found.');
    }
    require_once $classes[$name];
}

I am having hard time understanding what do i need to change to make this work?

You should change the following code:

if (!array_key_exists($name, $classes)) {
        die('Class "' . $name . '" not found.');
}
require_once $classes[$name];

into

if (array_key_exists($name, $classes)) {
        require_once $classes[$name];
}

If you stopped script when yout autoloader couldn't find class, only your autoloader is launched and if class is not found other autoloaders would not be launched to check if class exists

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