Reputation: 30521
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
Upvotes: 3
Views: 2781
Reputation: 42746
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
Upvotes: 1