3

It's my first time using autoloading but I keep getting the error saying my class can't be found:

Fatal error: Class 'Classes\Sunrise\DB' not found in my\sample\path\.sunrise.app\init.php on line 52

Edit: Line 52 is where I use a static method from the DB class. For some reason, PHP says it can't find the DB class.

My structure:

order (Folder)
    + index.php 
.sunrise.app
    + init.php
    + ordersetup.php
    + Classes (Folder)
        + Sunrise (Folder)
            + DB.php

The file init.php contains my autoloader and is included in the file ordersetup.php which in turn is included in order/index.php with include_once '../.sunrise.app/ordersetup.php'; In the file .sunrise.app/init.php I need to use the DB class but am met with the error shown above. What am I doing wrong?

My autoloader in .sunrise.app/init.php:

function my_autoload($class_name) {
  include $class_name. '.php';
}
spl_autoload_register('my_autoload');

My DB class in classes/Sunrise/DB.php:

<?php namespace Classes\Sunrise;

use PDO;

class DB { ... }

Edit: The folder Sunrise is under the folder Classes

5
  • You use Classes in the namespace, but classes in the folder hierarchy. Is this a linux system? Beware of correct casing. Use require instead of include to trigger a fatal error if the file does not exist. Commented Jul 7, 2013 at 18:35
  • My mistake, the classes folder is actually Classes. It's capitalized. Commented Jul 7, 2013 at 18:44
  • Your use PDO statement will call the autoloader for a class called PDO in the Classes\Sunrise namespace. If you want the PDO from the global namespace, you'll have to write use \PDO, where the leading `` references the global namespace Commented Jul 7, 2013 at 18:45
  • try removing the dot from the beginning of .sunrise.app in both the directory name as well as the autoloader. These file paths are generally ignored by apache. Or as a test try to require one of these files manually. Commented Jul 7, 2013 at 18:55
  • Removed the dot. Didn't work. Although I had my doubts since I don't think folder names can affect the outcome of paths. Commented Jul 7, 2013 at 19:01

1 Answer 1

1

since the seperators are \ you need to replace them with the correct directory seperator for the OS, if this is a linux os then they need to be /

function my_autoload($class_name) {
  $class_name = str_replace("\\","/",$class_name);
  require "../.sunrise.app/".$class_name. '.php'; //you have to use path relative to
                                                  // order or use a absolute path
                                                  // /var/www/.sunrise.app/
}

Also if linux system make sure the case of the classes match the case of the folder, since linux is case sensitive

Sign up to request clarification or add additional context in comments.

12 Comments

Sorry, I added that to my autoloader and it didn't work. This is interesting, if I comment out my autoloader and use the older (soon to be depracated) function __autoload($class_name) { require $class_name . '.php'; } in my init.php file, error disappears and my page loads. But I don't want to use that one.
@enchance works fine on my linux system, are you including the if clause? if so are you setting it true or false depending on what your system is?
Even with the if clause it still fails. With my current folder structure, how would you guys do it?
@enchance You need to exhibit some problem finding skills. echo $class_name inside the autoload to see if the autoloader gets called. Echo something in the class file to check if it is included. Use require instead of include. And please give us error messages instead of declaring that it does not work.
@enchance Is your script that has the autoload function being included before calls using the DB class?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.