Auto load of multiple directories

Asked

Viewed 132 times

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 {
   ...
}

Since libs are some classes that I will need throughout the program, that is, I will need to order the libs no doubt, but I will also need to order the models I need, this is a model mvc, the paths are in relation to the index. My question is, how do I do, inside the core/init.php, that the program makes load ALSO models as I instantiate them?

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');
  }
});

What’s more, I don’t recommend using this form to include dependencies.

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

https://getcomposer.org/doc/04-schema.md#psr-0

Hugs!

  • Why not recommend? Thank you worked

  • Precisely because it is an old technology. Nowadays we have Composer, a great dependency manager, I recommend researching about.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.