Loading Class files in Route

I am new to slim, but in getting things set up have not used the auto loader but have included the class files that are appropriate for each route as traditional php require_once structure. i.e.

$app->post(’/tickets’, function ($request, $response) {
require_once (‘includes/databaseObject.php’);
require_once (‘includes/ticket.php’);
require_once (‘includes/user.php’);
$decoded = $request->getAttribute(“Authorization”);

It works like charm and the speed is incredible. I like the design cause it gives me a visual roadmap for the route, but my question is, does this put the class in the container in the same manner as autoload? If so any obvious disadvantages to the way its set up now in terms of performance (secondary loading etc) ? . Absolutely amazing php revolution,… WOW

First autoloader doesn’t put the class in the container, auloader normally require the file of the class as you are doing but when code try to instantiate an object of the class
Let me say that requiring class is not “traditionally” is the “older way” when we didn’t have autoloading

One cons I see is that you have to require files in each route you need them.
In this case MVC is a particular case because it normally execute one route per request
about perfomance every time you require a file it is compiled and executed even if you don’t istantiate objects
IMHO this is the greatest advantage of using auoloading a class file is loaded only whe needed

Wow great answer, it helps, thanks. I am still trying to get my head around just what a “container” is and how and where it separates from a traditional php scope or operating environment. I assumed the speed comes from php getting the signal without involving the file system, kind of a direct pipe into memory,… would that be correct? (Whatever it is, on App Engine its impressive,… God Bless!).

Container is a way how to handle object dependencies and how to read them -> inject them

  • literally Container is used for “Dependency Injection”
  • if you want to understand it, I recommend implement your own really simple Container (there is standard interface for that, PSR11) - by using simple associative array and use array keys as object instance names (you can use just class name as name) ; it is really simple but for general usage it is of course slightly more complicated :slight_smile:

nice description of dependency injection is here:

I won’t elaborate on your container question, I think @jDolba has answered that quite nicely.

My suggestion would be that you put your classes in a namespace and use autoloader. Once you get used to this, there’s no going back, it makes things so much simpler and easier that you’ll find yourself wondering how you ever used PHP without autoloading. On top of this, modern PHP is all autoloader based, so you want to learn/understand how it works since it allows you effortlessly import & use other people’s code/libraries.