0

In file: deckclass.php , with DOCROOT set to /var/www/Proj/application/controllers

getting error: Fatal error: Class 'Cardclass' not found in /var/www/Proj/application/controllers/deckclass.php on line 38

Why is it searching current php file and not DOCROOT folder?

function __autoload($class_name) 
{
  //class directories
  $filename = DOCROOT .strtolower($class_name) . ".php";
  if ( file_exists($filename) )
  {
    require_once ($filename);
  }
  else {
    throw new Exception("Unable to load $class_name.");
  }
}

$card = new Cardclass();
4
  • Do you have any php file named as Cardclass in your controller folder? Commented Aug 10, 2014 at 18:54
  • yes it's called cardclass.php Commented Aug 10, 2014 at 18:57
  • Then Just do echo $filename; and check the output path Commented Aug 10, 2014 at 18:59
  • I deleted my answer as it was not a solution, I suppose for some reason the autoload method is not registered, otherwise you would be able to print the filename, unfortunately I don't know why. Commented Aug 10, 2014 at 19:43

1 Answer 1

0

You can use define('ROOT', dirname(__FILE__)); and define('DS', DIRECTORY_SEPARATOR);

Tip

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

So do code should look like this:

     <?php
        define('DS', DIRECTORY_SEPARATOR);
        define('ROOT', dirname(__FILE__)); //This is full path
        spl_autoload_register('Autoloader'); //You can use annonymous function here
        function Autoloader($class_name){
          $filename = ROOT.DS.strtolower($class_name).".php";
          var_dump($filename);
          if(file_exists($filename)){
            require_once ($filename);
          }else{
            throw new Exception("Unable to load ".$class_name."in".$filename);
          }
        }

        $card = new Cardclass();

With __autoload:

<?php
        define('DS', DIRECTORY_SEPARATOR);
        define('ROOT', dirname(__FILE__)); //This is full path
        function __autoload($class_name){
          $filename = ROOT.DS.strtolower($class_name).".php";
          var_dump($filename);
          if(strpos($class, 'CI_') !== 0) {

             if(file_exists($filename)){
               require_once ($filename);
            }else{
               throw new Exception("Unable to load ".$class_name."in".$filename);
            }
          }
        }

        $card = new Cardclass();
Sign up to request clarification or add additional context in comments.

5 Comments

Fatal error: Uncaught exception 'LogicException' with message 'Function 'Autoloader' not found on spl_autoload_register('Autoloader');
Unable to load the requested file: helpers/spl_autoload_helper.php i'm using PHP 5.3 too.
@Undermine2k What is result of ´echo 'Current PHP version: ' . phpversion();´
it says current version is 5.3.10-1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.