I welcome you back again to another video lecture in PHP.
And in this video lecture I'll go ahead and tissue about constructors in PHP.
So we have entered into classes and objects and in the previous lecture we are able to deal with these.
We created a class called car.
And we're able to model that class.
And we created the attributes of that class.
And we're able to create the object.
And then we use those attributes of the class to be able to model the car.
So I can see.
And you can also see that we have this car K1 and K2 right in here.
So created two objects of the car which is K1 and K2.
And now we are talking about constructor and what is constructor.
So constructor is a a piece of information or a function that is right inside the class which you just
created, and that it can always be called whenever you create.
Objects of that class.
So a constructor helps you to give more information to your object.
Now go ahead and say this is a class.
And right inside this class I'm going to create.
A function and any function I create here is going to be called a constructor.
So this is a function.
And when you create that function you're going to give two underscore for that to be a constructor.
So what are you going to do right now is create underscore for song underscore second one.
And then you go ahead and give constructor.
So you call construct and you don't call that constructor.
Just put it construct and then put in the parentheses.
And then go ahead and put open up close braces.
And whatever you write in here will be called out anytime you call this.
So if I go ahead and remove this then go ahead and remove this equal.
And then right in here and go ahead Unquiet and equal something.
Let me go on and say we created.
Okay.
So we created new cars because there's only just one car.
We created new cars.
And I'm going to put this put a break tag so it can be able to move to the next line.
The semicolon at the end.
And save this.
Now let's go ahead and refresh this.
We've created new cars and we created new cars.
So I told you that constructor is a piece of information or a function which you create inside a class.
And whenever you call an object or whenever you create an object, that's constructor is being called.
What happens here is that the first object you created here, called the constructor, and the second
object you created is called this constructor.
And just like our normal functions, when we created functions, you can pass any information into your
constructors.
Then go ahead and see that here we have a name.
Let's go ahead and say we.
this guy had to put this to be a name or what?
You have a name already for the car.
This car had and say we give this company.
All right.
So let's go.
That's the company name and the companies that make this.
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
show.
And then I want to echo the company that make that go ahead and say company and put a semicolon at the
end.
Now I will go to the first car and I will give the company.
Go ahead and pass in the name of the company.
And uh, I don't actually the company is still there, so I don't know how to put in there.
Let's go ahead and say is George.
All right.
And, uh, that is a company name.
And the next one is Nissan is already is already a company.
So I'm gonna say Steve.
And now let's go ahead and save this.
So if we go ahead and refresh this because we input in this the company name.
So it will always go here and check out whatever the first object has.
And that will be printed out.
And whatever the second object has that will be printed out.
Now let's go ahead and refresh this and check it out.
Judge Steve so you can actually go ahead and put a break tag inside so it can move to the next line.
So you can see that the very first object we created here called this constructor.
And the second object we have here called this constructor.
And that is why we have got Steve.
So a constructor is just an a function or piece of information which you input inside the class.
And whenever the object is being called it goes on to call the constructor.
Now you can see how many lines of code it takes us to write this, to create these two cars, or to
model the two cars.
So the constructor can help us to limit these lines of code because it's really too long.
So how can we be able to use constructor to model this two lines of code, or to fail to model these
two cars?
How are we able to use this constructor to model these two cars?
In order to make this to be more simpler and more easy for us.
Now let's go ahead and check it out.
So now what I'm going to do is I will go ahead and pass in all the attributes of everything we have
here, right inside this constructor.
So let me go ahead and get rid of this so that we can start this.
Now if I want to do this I should refactor each of these.
Let's say if I'm working with car one, I should refer to it as the current object I'm working with.
And if I want to refer to this as the current object, I'm going to use this.
So this keyword actually specifies the current object I'm working with.
So if I'm working on the car one that means this will always refer to the current object.
So D then I'll go ahead and copy the name.
And then I'm going to pass this one in here.
And before then I should go right inside idea.
I'm going to remove this company in which we put in here.
And then I'm going to set in the variables which I did here for k1, k2, k and the others I did for
k1 and K2.
Then we'll pass in the name variable, the variable and the color variable.
And for me to do that I say I can create a new one called name.
Then with semicolon and create another one called car E with semicolon.
Under the next one is a car color.
Now I go ahead.
I say car name is equal to then car name kind of at the end.
And then we also refer to the next one within this keyword.
That is the year.
Go ahead and say this is equal to Car.
You put a semicolon at the end and in the next one isn't this keyword.
So that anyone we are working on, we always know either car one or cat two.
So this column and this is equal to car column.
Now you see I always refer to each of these variable because you remember in the when we call the church
city it went on and get whatever that we have read in here.
So what it means is that any of the parameters you pass in here, the name, the year and the color,
you are going to pass that into your object.
So I'm going to get rid of this, uh, church, Steve or George I put in here.
And the very first one, the name is Toyota.
So for cow I'm going to pass in Toyota.
And the year.
So the year here is a string I can actually go ahead and pass that as an integer which is 2030.
And in the next one is uh the black which is the a color.
Go ahead and pass in that.
Now I can get rid of this.
Go ahead and remove this.
So I've done justice to cat one.
So for cat two I'll go ahead and get rid of this.
So for the name of the card I'll go ahead and pass in this one.
And the year is 2070.
And the color I'll go ahead and copy this.
So the reason why I'm doing this step by step is for you to know what we are working on.
We are not bringing something new is everything we have in here.
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
have here into just two lines of code.
So I'll go ahead and, uh, equal any one.
So for car one okay.
No, this is our car one.
Then we go ahead and, uh, print that out.
So if we want to print out the name of the car, can go ahead and say car one name.
Okay, so car one name and end and save this and this kind of refresh and check it out Toyota.
So you can see I'm able to do the same thing.
So if I change this to car two and save this.
I'll refresh this I have Nissan.
So if I change this to maybe the year again I save this.
I refresh this 2017 for car two.
So let me go ahead and say I want to modify the car.
One and I'm going to say I want to change the name of car one again and say car okay.
Car one.
And uh, let that be equal to okay.
We have the let's go ahead and see anything.
We can actually change that is equal to 4050.
So within the year 1457.
This.
And if I should go ahead and change this to count, go ahead and save this and refresh.
You can see I have 1450 words in here.
So I can actually modify that down here.
So the aim of using constructor is to help us to be able to give more information to an object.
And in that case, it also helps us to reduce the bunch of code we write on that object.
So you can see if I should go ahead and remove.
Let me get rid of this.
And I have those two lines of code here.
And if I refresh this it has gone back to 2030.
And if you change the color of this guy and I change the color and that is black.
So you can see the constructor plays a very big role, a vital role in PHP and is very important.
You know about that.
So why had I checked this out.
Practice with it.
And if you have any question, please go ahead and use the question and answer section.
And I'm going to get back to you as soon as possible.
Thank you so much.
And I'm going to see you in the next video lecture.