I don't know if this is exactly a problem in autoloading, but I am having this problem, here is my code:
index.php
require __DIR__ . '/app/autoload.php';
Folder structure:
index.php
app/
--autoload.php
autoload.php
function autoloader($className) {
// List Directories to Autoload Classes
$paths = array(
__DIR__ . '/system/',
__DIR__ . '/app/models/',
__DIR__ . '/app/dao/'
);
foreach($paths as $path) {
$file = $path . '/' . $className . '.php';
if (is_file($file))
include $file;
}
}
For some reason it doesn't work even I do:
__DIR__ . '../system/
... et al.
DIR in the autoload file will refer to /app.
try:
function autoloader($className) {
// List Directories to Autoload Classes
$paths = array(
'../system/',
'/models/',
'/dao/'
);
foreach($paths as $path) {
$file = $path . '/' . $className . '.php';
if (is_file($file))
require_once $file;
}
}
If that fails start echoing out paths and dir to see if they are referenced properly.
I would try it this way. Anywhere before the autoload.php is loaded (should be file which is always loaded, i guess in your case its index.php) i would define ROOT variable somwhere at begining of the script
/**
* Use the DS to separate the directories (just a shortcut)
*/
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
/**
* The full path to the directory which holds application files, without a trailing DS.
*/
if (!defined('ROOT')) {
define('ROOT', dirname(__FILE__));
}
and then in your autoload use ROOT
$paths = array(
ROOT . DS. 'system' . DS,
ROOT . DS. 'app' . DS . 'models' . DS,
ROOT . DS. 'app' . 'dao' . DS
);