Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am using following function to autoload classes, it works fine if i am using only one directory called 'classes' however when i try to use Smarty lib also it fails and give me the error

Fatal error: Class 'Database' not found in /home/...

for ex:

require_once(DOC_ROOT."/libs/Smarty.class.php");
function __autoload($class_name) {   
    require_once( CLASS_LIB."/".$class_name . '.class.php');
}
$db = new Database();
$session=new Session();
$smarty = new Smarty();

but if i do this it give me the error unable to load the smarty class..

function __autoload($class_name) {   
    require_once( CLASS_LIB."/".$class_name . '.class.php');
    require_once(DOC_ROOT."/libs/Smarty.class.php");
}

$db = new Database();
$session=new Session();
$smarty = new Smarty();

Warning: require_once(/home/.../classes/Smarty_Internal_TemplateCompilerBase.class.php) [function.require-once]: failed to open stream: No such file or directory in /home/.../includes/init.php 

Any idea what i am i doing wrong here ? i need to be able to load classes directory automatically but needs to make sure i dont loose smarty path too..

share|improve this question
    
I'd advise against using __autoload as it only lets you register a single autoload method. The addition of spl_autoload_register has pretty much made __autoload redundant, and provides more flexibility. –  GordonM Jul 16 '12 at 10:50
add comment

2 Answers

Several solutions come to mind.

  • Use include_once instead of require_once to avoid the error.
  • Check for the existence of the file before doing the require_once.
  • Put in an if statement detects Smarty and special cases the code.
  • Just include the Smarty classes manually.
  • (Best) Use spl_autoload_register twice, once for your classes and once for the Smarty classes. __autoload is deprecated. Spl_autoload_register should handle the problem. http://php.net/manual/en/language.oop5.autoload.php
share|improve this answer
    
+1 for spl_autoload –  GordonM Jul 16 '12 at 10:50
add comment
up vote 0 down vote accepted

I solve the problem spl_autoload_register function two times.

below is the code for reference.

spl_autoload_register('load_api');
spl_autoload_register('load_lib');

function load_api($class){
    if(!file_exists(CLASS_LIB."/".$class. '.class.php') )
        return false;
    require_once( CLASS_LIB."/".$class. '.class.php');
    return true;
}
function load_lib($class){
    if( !file_exists(DOC_ROOT."/libs/Smarty.class.php") )
        return false;
    require_once(DOC_ROOT."/libs/Smarty.class.php");
    return true;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.