Okay, so it is time that we create our blog project.
So we're going to start by creating a directory, initializing composer and Autoloading.
And throughout the process I'm going to explain why we are using composer for that.
And what exactly is Autoloading.
We are going to also create a scaffolding of the project, some essential folders.
So essential structure of the project would be created.
Now first Waltz composer.
It is a dependency manager for PHP.
So if you use any external libraries or frameworks, composer is the de facto tool to manage those dependencies,
though I try to not use any dependencies as long as possible in this course.
So we're just going to use composer to help us set up auto loading and then later on to quickly and
easily run some command line scripts.
So the first step would be to create the folder that will contain this project.
I'm doing this throughout the command line.
You can do it manually.
Makes no difference.
I'm going to call this blog.
OOP as object oriented programming because this is what we will be doing.
Next up we need to go into this folder.
And at this point you should also have the command line command prompt PowerShell.
Whatever you are using in whatever system open inside that directory because there is a command to run.
We need to initialize composer.
This is done by just typing composer init inside this directory.
This should initialize a config generator.
We're going to be asked some questions, so I will suggest what you should do.
If we are not changing anything then just hit enter return to confirm the defaults.
Like in this case, I'm just gonna stick with the suggested name.
I'm not typing any description, I'm just hitting enter all the time.
Now.
At this point I also confirm with enter, and this is something that we need to provide some specific
input on.
We don't need any dependencies.
That's why I'd like you to type no at this point.
And again no because we also don't need any development dependencies.
That's not what we need composer for.
And also with this question just hit enter.
We want to skip that.
I'm gonna be configuring the auto loading naming and namespaces manually.
And I'm going to be explaining that.
So just type N and confirm with enter.
That's the minimum composer file that we would like to generate.
Confirm the generation by hitting enter again and we should have this file.
Now at this point I'd like you to open this directory inside the code editor.
I'm using the terminal shortcut.
We only have this composer JSON file here.
Let's open it.
Now let's manually add the auto loading section by adding the auto load entry.
Next up we are saying that we want to use the PSR four standard.
That's just one of the PHP standards that defines what's the best way to do things.
In this case that's a standard describing how to auto load files.
Now you need to bear with me.
Now let me define two namespaces.
Everything that starts with an app will be located inside the app directory.
On the other hand, everything starting with core.
So the generic framework logic will go into the core directory.
Now at this point I'd like you to open the terminal.
It can be the terminal inside VSCode or just your separate terminal command prompt application PowerShell,
whatever you are using.
And type this composer dump hyphen Autoload.
This should generate the Autoload files and the vendor folder.
There it is, the Autoload php file.
This is the one we need to make outloading work.
So with the auto loading, if you need some classes, instead of having to require or include them manually
every single time, you just use namespaces and then you tell PHP from which namespace a class or function
should be imported, and then PHP is automatically loading the required PHP files, which improves performance.
So our next job is to create the app and core folders so we can see what I just described in action.
Inside this core folder let's create a router PHP file.
This is also important to understand that when we will be using classes, the file name needs to match
the class name.
This is how the classes are resolved using Namespaces, so you still have to use the PHP tag.
But next up what follows is the namespace declaration.
Can you guess what should be the namespace here?
It's a router class inside the core folder, so looking at the composer file it should be core.
So let's use this namespace.
That's everything.
That's just core.
Now let's define the router class.
And the only thing I'd like to add here for now is a constructor that echoes something when the class
is created.
Something like works.
So we can check if this really works.
Now let's create the main file of this app.
So we always have the public folder.
This is the public facing directory which has the app starting point Called index.php.
Here, let me add the strict type declaration.
This is strict types equals one.
And the first step here is to require this auto loading file generated by composer.
So I do require once using the current directory adding a forward slash, going one directory up then
to the vendor folder autoload php.
By including this file, I'm giving my app auto loading functionality automatically generated and handled
by composer.
Now I can use classes from different namespaces.
This is how you tell PHP.
It should load some class by just using a namespace or using a class from a namespace.
So this is for example core router.
This would just make PHP auto load the specific file with this class definition.
I can now create a router class.
And next up we're going to just start the server to see if everything worked as we expected to.
So open the terminal and type php hyphen S.
It needs to be a capital S the domain.
It can be localhost 8000.
Just make sure no other project is running at this port.
And then additional parameter t lowercase t pointing to the public directory, which would just let
php find the index file by itself.
So apparently I have something running on this port.
Let me change it to something else 8001.
Now let's jump to this page and we see that everything has worked fine.
So let's summarize what we've learned in PHP.
There is a process called autoloading, which takes the burden of figuring out where the classes and
functions are from your back.
Instead, you only worry about the class names that you'd like to use or function names.
You declare the namespaces inside those classes and functions that you use, and you can use composer
to generate autoloading files for you.
And then you only tell PHP which classes you need in a particular file, or which functions do you need
in a file.
And PHP will take care of loading the necessary PHP files, making everything more efficient.