WEBVTT

00:00.230 --> 00:04.730
Okay, so it is time that we create our blog project.

00:04.760 --> 00:12.560
So we're going to start by creating a directory, initializing composer and Autoloading.

00:12.560 --> 00:18.020
And throughout the process I'm going to explain why we are using composer for that.

00:18.020 --> 00:21.140
And what exactly is Autoloading.

00:21.140 --> 00:28.460
We are going to also create a scaffolding of the project, some essential folders.

00:28.460 --> 00:32.750
So essential structure of the project would be created.

00:32.780 --> 00:34.970
Now first Waltz composer.

00:35.000 --> 00:39.260
It is a dependency manager for PHP.

00:39.500 --> 00:48.560
So if you use any external libraries or frameworks, composer is the de facto tool to manage those dependencies,

00:48.560 --> 00:54.710
though I try to not use any dependencies as long as possible in this course.

00:54.710 --> 01:03.170
So we're just going to use composer to help us set up auto loading and then later on to quickly and

01:03.170 --> 01:07.280
easily run some command line scripts.

01:07.340 --> 01:12.320
So the first step would be to create the folder that will contain this project.

01:12.350 --> 01:14.720
I'm doing this throughout the command line.

01:14.720 --> 01:16.190
You can do it manually.

01:16.190 --> 01:17.840
Makes no difference.

01:17.870 --> 01:20.240
I'm going to call this blog.

01:20.270 --> 01:26.300
OOP as object oriented programming because this is what we will be doing.

01:26.360 --> 01:29.900
Next up we need to go into this folder.

01:29.900 --> 01:35.840
And at this point you should also have the command line command prompt PowerShell.

01:35.840 --> 01:44.270
Whatever you are using in whatever system open inside that directory because there is a command to run.

01:44.300 --> 01:47.270
We need to initialize composer.

01:47.300 --> 01:53.660
This is done by just typing composer init inside this directory.

01:54.080 --> 01:58.040
This should initialize a config generator.

01:58.370 --> 02:03.410
We're going to be asked some questions, so I will suggest what you should do.

02:03.440 --> 02:09.470
If we are not changing anything then just hit enter return to confirm the defaults.

02:09.470 --> 02:14.360
Like in this case, I'm just gonna stick with the suggested name.

02:15.590 --> 02:20.840
I'm not typing any description, I'm just hitting enter all the time.

02:20.840 --> 02:21.200
Now.

02:21.200 --> 02:28.550
At this point I also confirm with enter, and this is something that we need to provide some specific

02:28.550 --> 02:29.330
input on.

02:29.330 --> 02:31.610
We don't need any dependencies.

02:31.610 --> 02:34.430
That's why I'd like you to type no at this point.

02:34.430 --> 02:40.850
And again no because we also don't need any development dependencies.

02:40.850 --> 02:43.460
That's not what we need composer for.

02:43.760 --> 02:47.180
And also with this question just hit enter.

02:47.210 --> 02:48.740
We want to skip that.

02:48.980 --> 02:54.740
I'm gonna be configuring the auto loading naming and namespaces manually.

02:54.740 --> 02:56.600
And I'm going to be explaining that.

02:56.600 --> 03:00.380
So just type N and confirm with enter.

03:00.410 --> 03:04.460
That's the minimum composer file that we would like to generate.

03:04.460 --> 03:10.880
Confirm the generation by hitting enter again and we should have this file.

03:11.180 --> 03:16.880
Now at this point I'd like you to open this directory inside the code editor.

03:16.910 --> 03:19.520
I'm using the terminal shortcut.

03:20.330 --> 03:23.270
We only have this composer JSON file here.

03:23.270 --> 03:24.590
Let's open it.

03:26.540 --> 03:33.500
Now let's manually add the auto loading section by adding the auto load entry.

03:36.620 --> 03:41.750
Next up we are saying that we want to use the PSR four standard.

03:41.780 --> 03:47.210
That's just one of the PHP standards that defines what's the best way to do things.

03:47.210 --> 03:52.370
In this case that's a standard describing how to auto load files.

03:52.700 --> 03:54.680
Now you need to bear with me.

03:54.710 --> 03:57.530
Now let me define two namespaces.

03:57.560 --> 04:04.400
Everything that starts with an app will be located inside the app directory.

04:06.290 --> 04:09.110
On the other hand, everything starting with core.

04:09.110 --> 04:16.730
So the generic framework logic will go into the core directory.

04:17.930 --> 04:20.900
Now at this point I'd like you to open the terminal.

04:20.900 --> 04:29.900
It can be the terminal inside VSCode or just your separate terminal command prompt application PowerShell,

04:29.930 --> 04:31.730
whatever you are using.

04:31.730 --> 04:40.490
And type this composer dump hyphen Autoload.

04:42.020 --> 04:46.400
This should generate the Autoload files and the vendor folder.

04:46.940 --> 04:49.550
There it is, the Autoload php file.

04:49.880 --> 04:54.440
This is the one we need to make outloading work.

04:54.440 --> 05:02.570
So with the auto loading, if you need some classes, instead of having to require or include them manually

05:02.570 --> 05:11.240
every single time, you just use namespaces and then you tell PHP from which namespace a class or function

05:11.240 --> 05:19.580
should be imported, and then PHP is automatically loading the required PHP files, which improves performance.

05:21.290 --> 05:29.990
So our next job is to create the app and core folders so we can see what I just described in action.

05:31.460 --> 05:36.680
Inside this core folder let's create a router PHP file.

05:36.680 --> 05:44.300
This is also important to understand that when we will be using classes, the file name needs to match

05:44.300 --> 05:45.320
the class name.

05:45.320 --> 05:52.700
This is how the classes are resolved using Namespaces, so you still have to use the PHP tag.

05:52.730 --> 05:57.320
But next up what follows is the namespace declaration.

05:57.320 --> 06:00.350
Can you guess what should be the namespace here?

06:00.350 --> 06:09.710
It's a router class inside the core folder, so looking at the composer file it should be core.

06:10.790 --> 06:12.680
So let's use this namespace.

06:12.680 --> 06:13.790
That's everything.

06:13.790 --> 06:15.080
That's just core.

06:15.110 --> 06:17.990
Now let's define the router class.

06:19.160 --> 06:27.980
And the only thing I'd like to add here for now is a constructor that echoes something when the class

06:28.010 --> 06:29.120
is created.

06:29.120 --> 06:30.920
Something like works.

06:30.920 --> 06:33.860
So we can check if this really works.

06:35.360 --> 06:38.090
Now let's create the main file of this app.

06:38.330 --> 06:40.760
So we always have the public folder.

06:40.760 --> 06:49.340
This is the public facing directory which has the app starting point Called index.php.

06:51.110 --> 06:55.100
Here, let me add the strict type declaration.

06:55.100 --> 06:58.280
This is strict types equals one.

06:58.370 --> 07:04.820
And the first step here is to require this auto loading file generated by composer.

07:05.000 --> 07:14.780
So I do require once using the current directory adding a forward slash, going one directory up then

07:14.780 --> 07:17.660
to the vendor folder autoload php.

07:17.990 --> 07:27.830
By including this file, I'm giving my app auto loading functionality automatically generated and handled

07:27.830 --> 07:28.970
by composer.

07:29.000 --> 07:34.250
Now I can use classes from different namespaces.

07:34.250 --> 07:36.560
This is how you tell PHP.

07:36.590 --> 07:43.640
It should load some class by just using a namespace or using a class from a namespace.

07:43.640 --> 07:47.120
So this is for example core router.

07:47.360 --> 07:54.200
This would just make PHP auto load the specific file with this class definition.

07:55.400 --> 07:58.010
I can now create a router class.

08:00.740 --> 08:07.250
And next up we're going to just start the server to see if everything worked as we expected to.

08:08.840 --> 08:13.880
So open the terminal and type php hyphen S.

08:13.880 --> 08:18.710
It needs to be a capital S the domain.

08:18.710 --> 08:21.230
It can be localhost 8000.

08:21.260 --> 08:25.790
Just make sure no other project is running at this port.

08:26.660 --> 08:34.970
And then additional parameter t lowercase t pointing to the public directory, which would just let

08:35.150 --> 08:38.210
php find the index file by itself.

08:38.210 --> 08:42.170
So apparently I have something running on this port.

08:42.830 --> 08:46.370
Let me change it to something else 8001.

08:46.400 --> 08:52.220
Now let's jump to this page and we see that everything has worked fine.

08:53.120 --> 08:56.180
So let's summarize what we've learned in PHP.

08:56.210 --> 09:04.130
There is a process called autoloading, which takes the burden of figuring out where the classes and

09:04.130 --> 09:06.590
functions are from your back.

09:06.620 --> 09:12.830
Instead, you only worry about the class names that you'd like to use or function names.

09:13.340 --> 09:20.930
You declare the namespaces inside those classes and functions that you use, and you can use composer

09:20.930 --> 09:24.950
to generate autoloading files for you.

09:24.950 --> 09:33.110
And then you only tell PHP which classes you need in a particular file, or which functions do you need

09:33.110 --> 09:33.980
in a file.

09:33.980 --> 09:41.990
And PHP will take care of loading the necessary PHP files, making everything more efficient.
