Php autoload project

I’ve a project like this:

project

Now i want to autoload all the php files in the folder classes and sub folders.

I can do that with this:

$dirs = array(
   CMS_ROOT.'/classes',
   CMS_ROOT.'/classes/layout',
   CMS_ROOT.'/classes/layout/pages'
);
foreach( $array as $dir) {
  foreach ( glob( $dir."/*.php" ) as $filename ) {
    require_once $filename;
  }
}

But i dont like this. For example.

“layout/pages/a.php” extends “layout/pages/b.php”

Now i get an error because a.php was loaded first. How do you people load your project files? Classes?

SOLVED :)

This is my code now:

spl_autoload_register('autoloader');
function autoloader($className) {
    $className = str_replace('cms_', '', $className);
    $className = str_replace('_', '/', $className);

    $file = CLASSES.'/'.$className.'.php';
    if( file_exists( $file ) ) {
    require_once $file;
    }
}

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories