Auto load multiple directories

0

I have these functions:

core / init.php:

...
spl_autoload_register(function($class) {
   require_once ('libs/'.$class.'.php');
});
---

controllers / Images_Controller.php

require_once "core/init.php";
require_once "models/Image.php";
require_once "models/User.php";

class Images_Controller extends Controller {
   ...
}

As libs are some classes that I will need throughout the program, that is, I will need to request the libs without a doubt, but I will also need to request the models I need, this is a mvc model, the paths are relative to index. My question is: how do I do within core/init.php that the program does load ALSO of the models as I instantiate them?

    
asked by anonymous 14.09.2015 / 18:26

1 answer

2
spl_autoload_register(function($class) {
  if(file_exists('libs/'.$class.'.php') {
   require_once ('libs/'.$class.'.php');
  } elseif(file_exists('models/'.$class.'.php') {
   require_once ('models/'.$class.'.php');
  }
});

Furthermore, I do not recommend using this form to include dependencies.

Do you know composer? I recommend leaving it to him to do his class map.

link

Hugs!

    
14.09.2015 / 18:53