WEBVTT

00:00.440 --> 00:03.860
I welcome you back again to another video lecture in PHP.

00:03.860 --> 00:09.380
And in this video lecture I'll go ahead and tissue about constructors in PHP.

00:16.040 --> 00:22.610
So we have entered into classes and objects and in the previous lecture we are able to deal with these.

00:22.610 --> 00:25.880
We created a class called car.

00:25.880 --> 00:27.950
And we're able to model that class.

00:27.950 --> 00:30.620
And we created the attributes of that class.

00:30.620 --> 00:32.510
And we're able to create the object.

00:32.510 --> 00:38.300
And then we use those attributes of the class to be able to model the car.

00:38.900 --> 00:40.490
So I can see.

00:40.490 --> 00:45.770
And you can also see that we have this car K1 and K2 right in here.

00:45.800 --> 00:49.790
So created two objects of the car which is K1 and K2.

00:49.820 --> 00:54.110
And now we are talking about constructor and what is constructor.

00:54.200 --> 01:03.560
So constructor is a a piece of information or a function that is right inside the class which you just

01:03.570 --> 01:08.040
created, and that it can always be called whenever you create.

01:08.070 --> 01:09.810
Objects of that class.

01:09.810 --> 01:15.300
So a constructor helps you to give more information to your object.

01:15.330 --> 01:17.670
Now go ahead and say this is a class.

01:17.670 --> 01:20.670
And right inside this class I'm going to create.

01:20.700 --> 01:25.440
A function and any function I create here is going to be called a constructor.

01:25.440 --> 01:27.360
So this is a function.

01:27.360 --> 01:33.720
And when you create that function you're going to give two underscore for that to be a constructor.

01:33.720 --> 01:38.850
So what are you going to do right now is create underscore for song underscore second one.

01:38.850 --> 01:43.470
And then you go ahead and give constructor.

01:43.500 --> 01:48.090
So you call construct and you don't call that constructor.

01:48.120 --> 01:52.920
Just put it construct and then put in the parentheses.

01:52.920 --> 01:55.860
And then go ahead and put open up close braces.

01:55.860 --> 02:00.540
And whatever you write in here will be called out anytime you call this.

02:00.540 --> 02:03.990
So if I go ahead and remove this then go ahead and remove this equal.

02:03.990 --> 02:08.220
And then right in here and go ahead Unquiet and equal something.

02:08.220 --> 02:12.780
Let me go on and say we created.

02:15.300 --> 02:15.690
Okay.

02:15.840 --> 02:19.140
So we created new cars because there's only just one car.

02:19.170 --> 02:20.940
We created new cars.

02:20.940 --> 02:25.620
And I'm going to put this put a break tag so it can be able to move to the next line.

02:25.770 --> 02:26.760
The semicolon at the end.

02:26.760 --> 02:27.840
And save this.

02:27.870 --> 02:29.850
Now let's go ahead and refresh this.

02:29.910 --> 02:32.610
We've created new cars and we created new cars.

02:32.610 --> 02:40.290
So I told you that constructor is a piece of information or a function which you create inside a class.

02:40.290 --> 02:47.760
And whenever you call an object or whenever you create an object, that's constructor is being called.

02:47.760 --> 02:53.100
What happens here is that the first object you created here, called the constructor, and the second

02:53.130 --> 02:56.100
object you created is called this constructor.

02:57.420 --> 03:02.400
And just like our normal functions, when we created functions, you can pass any information into your

03:02.400 --> 03:03.180
constructors.

03:03.210 --> 03:06.420
Then go ahead and see that here we have a name.

03:06.450 --> 03:08.550
Let's go ahead and say we.

03:08.620 --> 03:12.340
this guy had to put this to be a name or what?

03:12.340 --> 03:14.020
You have a name already for the car.

03:14.050 --> 03:17.920
This car had and say we give this company.

03:18.520 --> 03:18.820
All right.

03:18.850 --> 03:19.330
So let's go.

03:19.330 --> 03:22.810
That's the company name and the companies that make this.

03:22.810 --> 03:30.550
So what I'm going to do is I will go ahead and let me comment this so that this will not be able to

03:30.550 --> 03:30.880
show.

03:30.880 --> 03:39.730
And then I want to echo the company that make that go ahead and say company and put a semicolon at the

03:39.730 --> 03:40.150
end.

03:40.180 --> 03:43.240
Now I will go to the first car and I will give the company.

03:43.360 --> 03:45.850
Go ahead and pass in the name of the company.

03:45.850 --> 03:50.560
And uh, I don't actually the company is still there, so I don't know how to put in there.

03:50.590 --> 03:52.960
Let's go ahead and say is George.

03:53.050 --> 03:53.920
All right.

03:53.920 --> 03:56.230
And, uh, that is a company name.

03:56.230 --> 03:59.500
And the next one is Nissan is already is already a company.

03:59.500 --> 04:01.150
So I'm gonna say Steve.

04:01.390 --> 04:03.190
And now let's go ahead and save this.

04:03.190 --> 04:10.870
So if we go ahead and refresh this because we input in this the company name.

04:10.870 --> 04:15.020
So it will always go here and check out whatever the first object has.

04:15.050 --> 04:16.430
And that will be printed out.

04:16.430 --> 04:19.130
And whatever the second object has that will be printed out.

04:19.160 --> 04:21.320
Now let's go ahead and refresh this and check it out.

04:21.590 --> 04:28.070
Judge Steve so you can actually go ahead and put a break tag inside so it can move to the next line.

04:28.820 --> 04:33.500
So you can see that the very first object we created here called this constructor.

04:33.500 --> 04:37.460
And the second object we have here called this constructor.

04:37.460 --> 04:39.770
And that is why we have got Steve.

04:39.770 --> 04:47.000
So a constructor is just an a function or piece of information which you input inside the class.

04:47.000 --> 04:51.680
And whenever the object is being called it goes on to call the constructor.

04:51.710 --> 04:58.280
Now you can see how many lines of code it takes us to write this, to create these two cars, or to

04:58.310 --> 04:59.750
model the two cars.

04:59.750 --> 05:05.210
So the constructor can help us to limit these lines of code because it's really too long.

05:05.240 --> 05:13.160
So how can we be able to use constructor to model this two lines of code, or to fail to model these

05:13.160 --> 05:13.790
two cars?

05:13.850 --> 05:16.700
How are we able to use this constructor to model these two cars?

05:16.730 --> 05:21.110
In order to make this to be more simpler and more easy for us.

05:21.140 --> 05:23.000
Now let's go ahead and check it out.

05:24.080 --> 05:32.750
So now what I'm going to do is I will go ahead and pass in all the attributes of everything we have

05:32.750 --> 05:34.160
here, right inside this constructor.

05:34.190 --> 05:38.240
So let me go ahead and get rid of this so that we can start this.

05:38.270 --> 05:41.870
Now if I want to do this I should refactor each of these.

05:41.900 --> 05:47.150
Let's say if I'm working with car one, I should refer to it as the current object I'm working with.

05:47.180 --> 05:53.030
And if I want to refer to this as the current object, I'm going to use this.

05:53.030 --> 05:58.820
So this keyword actually specifies the current object I'm working with.

05:58.850 --> 06:06.710
So if I'm working on the car one that means this will always refer to the current object.

06:06.710 --> 06:10.820
So D then I'll go ahead and copy the name.

06:10.820 --> 06:13.100
And then I'm going to pass this one in here.

06:13.100 --> 06:16.770
And before then I should go right inside idea.

06:16.800 --> 06:19.470
I'm going to remove this company in which we put in here.

06:19.470 --> 06:28.860
And then I'm going to set in the variables which I did here for k1, k2, k and the others I did for

06:28.860 --> 06:30.180
k1 and K2.

06:30.210 --> 06:34.830
Then we'll pass in the name variable, the variable and the color variable.

06:34.830 --> 06:39.690
And for me to do that I say I can create a new one called name.

06:40.560 --> 06:49.590
Then with semicolon and create another one called car E with semicolon.

06:49.590 --> 06:54.330
Under the next one is a car color.

06:54.360 --> 06:56.460
Now I go ahead.

06:56.460 --> 07:06.180
I say car name is equal to then car name kind of at the end.

07:06.180 --> 07:11.250
And then we also refer to the next one within this keyword.

07:12.960 --> 07:14.580
That is the year.

07:15.960 --> 07:19.980
Go ahead and say this is equal to Car.

07:20.220 --> 07:25.680
You put a semicolon at the end and in the next one isn't this keyword.

07:25.680 --> 07:30.900
So that anyone we are working on, we always know either car one or cat two.

07:30.930 --> 07:38.490
So this column and this is equal to car column.

07:38.610 --> 07:44.850
Now you see I always refer to each of these variable because you remember in the when we call the church

07:44.850 --> 07:48.540
city it went on and get whatever that we have read in here.

07:48.540 --> 07:56.250
So what it means is that any of the parameters you pass in here, the name, the year and the color,

07:56.280 --> 07:58.770
you are going to pass that into your object.

07:58.770 --> 08:03.390
So I'm going to get rid of this, uh, church, Steve or George I put in here.

08:03.390 --> 08:07.560
And the very first one, the name is Toyota.

08:07.560 --> 08:10.530
So for cow I'm going to pass in Toyota.

08:10.650 --> 08:11.910
And the year.

08:11.910 --> 08:17.430
So the year here is a string I can actually go ahead and pass that as an integer which is 2030.

08:17.430 --> 08:22.180
And in the next one is uh the black which is the a color.

08:22.210 --> 08:24.910
Go ahead and pass in that.

08:24.910 --> 08:27.670
Now I can get rid of this.

08:27.700 --> 08:28.660
Go ahead and remove this.

08:28.660 --> 08:31.540
So I've done justice to cat one.

08:31.540 --> 08:34.180
So for cat two I'll go ahead and get rid of this.

08:34.180 --> 08:38.800
So for the name of the card I'll go ahead and pass in this one.

08:40.120 --> 08:45.580
And the year is 2070.

08:45.580 --> 08:48.520
And the color I'll go ahead and copy this.

08:48.520 --> 08:52.690
So the reason why I'm doing this step by step is for you to know what we are working on.

08:52.690 --> 08:55.750
We are not bringing something new is everything we have in here.

08:55.780 --> 09:04.810
So now I'll get rid of this and the what I need to do now is now I have been able to reduce what we

09:04.810 --> 09:07.480
have here into just two lines of code.

09:07.480 --> 09:11.380
So I'll go ahead and, uh, equal any one.

09:11.380 --> 09:15.460
So for car one okay.

09:15.490 --> 09:18.520
No, this is our car one.

09:18.550 --> 09:23.860
Then we go ahead and, uh, print that out.

09:23.860 --> 09:29.930
So if we want to print out the name of the car, can go ahead and say car one name.

09:30.800 --> 09:37.970
Okay, so car one name and end and save this and this kind of refresh and check it out Toyota.

09:38.210 --> 09:40.460
So you can see I'm able to do the same thing.

09:40.460 --> 09:43.490
So if I change this to car two and save this.

09:43.520 --> 09:45.590
I'll refresh this I have Nissan.

09:45.770 --> 09:52.820
So if I change this to maybe the year again I save this.

09:52.850 --> 09:56.900
I refresh this 2017 for car two.

09:57.620 --> 10:02.930
So let me go ahead and say I want to modify the car.

10:02.960 --> 10:10.760
One and I'm going to say I want to change the name of car one again and say car okay.

10:10.850 --> 10:11.810
Car one.

10:12.260 --> 10:17.210
And uh, let that be equal to okay.

10:17.210 --> 10:20.060
We have the let's go ahead and see anything.

10:20.090 --> 10:25.610
We can actually change that is equal to 4050.

10:25.610 --> 10:28.280
So within the year 1457.

10:28.280 --> 10:28.640
This.

10:28.640 --> 10:35.510
And if I should go ahead and change this to count, go ahead and save this and refresh.

10:35.570 --> 10:38.180
You can see I have 1450 words in here.

10:38.270 --> 10:40.940
So I can actually modify that down here.

10:40.940 --> 10:47.810
So the aim of using constructor is to help us to be able to give more information to an object.

10:47.810 --> 10:54.530
And in that case, it also helps us to reduce the bunch of code we write on that object.

10:54.530 --> 10:56.600
So you can see if I should go ahead and remove.

10:56.750 --> 10:57.860
Let me get rid of this.

10:57.860 --> 11:00.170
And I have those two lines of code here.

11:00.170 --> 11:02.810
And if I refresh this it has gone back to 2030.

11:02.810 --> 11:09.350
And if you change the color of this guy and I change the color and that is black.

11:09.980 --> 11:16.640
So you can see the constructor plays a very big role, a vital role in PHP and is very important.

11:16.640 --> 11:17.570
You know about that.

11:17.570 --> 11:19.010
So why had I checked this out.

11:19.010 --> 11:19.850
Practice with it.

11:19.850 --> 11:23.630
And if you have any question, please go ahead and use the question and answer section.

11:23.630 --> 11:26.120
And I'm going to get back to you as soon as possible.

11:26.150 --> 11:27.050
Thank you so much.

11:27.050 --> 11:30.020
And I'm going to see you in the next video lecture.
