‫Okay.
‫So what I want to do now is create an auto loader.
‫And we're going to start off with just a simple custom one using a specific PHP function.
‫And then we're going to use composer to set up a PSR for auto loader, which is a bit more complex and
‫will help us with what we need later on, especially when we get into namespaces.
‫So an auto loader will allow us to have a bunch of classes in this framework folder and not have to
‫do this, require them individually.
‫So basically we can make it.
‫So if we create a class called whatever database and it matches the file name as it does in this case
‫it'll be auto loaded and we won't have to require it.
‫So there's just a couple lines of code that we can write to make that happen.
‫And to do it we're going to use the SPL underscore auto load underscore register function okay.
‫So this allows us to create a custom auto loader.
‫And it takes in a function.
‫And that function takes in a class.
‫Okay.
‫And then what I want to do here is create a path variable.
‫And we're going to use our base path.
‫And we want to look inside the framework folder.
‫So framework slash and then we want to concatenate whatever that class is and then concatenate the dot
‫PHP extension.
‫Okay.
‫Because this will just be the class name.
‫And we basically want to just map that to a file name inside the framework folder.
‫And we want to load that or require that if it exists.
‫So let's say if.
‫And we'll say if file underscore exists.
‫Path.
‫Then we simply want to require.
‫That path.
‫And that's it.
‫Now, I should be able to get rid of these.
‫And I mean, if you only planned on having two classes or two files in here ever, then you don't need
‫to do this.
‫But you want to be able to scale your application.
‫So this will allow you to have as many classes in here as you want and not have to require them individually.
‫So if I save that and reload, you'll see it still works.
‫Since we're seeing the data here, that means obviously the database class is being included or required
‫and it's being used.
‫If I can go to separate routes, then we know the router is being used.
‫And just to show you even further, if we create a new class, let's just call it test dot PHP.
‫And I'm going to delete this after and let's say class test.
‫Remember you want to name it the same as the file name.
‫And then let's create a constructor which runs automatically when when it's instantiated.
‫And we'll just do a die and we'll say test ran.
‫Now.
‫If I reload, it's not going to do anything because we didn't instantiate it yet.
‫So if I go to my index.php and I say, let's say test equals new test, and I didn't do a require test
‫anywhere, but let's go ahead and run that and you'll see tests ran.
‫So my constructor is running.
‫My class is being loaded because of this right here.
‫And I didn't have to say require framework test.
‫So I can just create as many classes as I want and they'll just work.
‫Okay.
‫I'm going to get rid of that because I just wanted to give you that example.
‫You can delete the test file if you created it.
‫So now we have a custom auto loader.
‫Now there are some issues that will have if we keep it this way, and we start using namespaces and
‫doing some of the things we want to do.
‫So in the next video, we're going to use what's called a PSR for auto loader.