I will call you back again to another video lecture in PHP.
And in this video lecture we go ahead and look at getters and setters.
So getters and setters are very important aspects of PHP development.
And we're going to look at it right in here.
Now I have just modified what we did in the previous lecture because we have talked about cars and we
keep on talking about cars is going to be boring.
And some people will be like, why don't you change to something else?
For us to have a better understanding of what objects and classes and object functions and code mean.
So that's what tool getters and setters have to make some modifications by changing this into another
thing altogether, or using every other thing we've learnt in the previous lectures.
Now we're talking about a rainbow right in here.
And we know that rainbow has seven colors, and these colors can never be modified because it is always
there.
Now we have red in here.
I created a statement.
Rainier has seven colors red, orange, yellow, green, blue, indigo, and violet and these are the
only colors that is here.
But as this program is this program, we can actually modify that.
Let's go ahead and refresh this.
We have red color which is red in here.
Now the next thing I'm going to talk about is the getters and setters.
You can see that I'm using var right in here which is the variable I can actually go ahead and change
this to public.
So if we are using public it means that this is now accessible to the public.
And everybody can make changes to here.
So if I go ahead and refresh this we can see that my code is still working fine.
So I've been using VAR in the previous lectures and I can actually use public day, but I left that
so we can talk about it in this lecture.
Now you can see I have public and making this accessible to every kind of person out there.
It means that someone can come back and go over here and tell that the color of this has been modified
by injecting some things into this code, you can say that this is equal to.
It creates another.
The variable symbol is known as hyphen greater than color.
And the person can actually change these and say that we have block in symbol.
And now if we go ahead and run this code check it out.
You can see that we have black and you'll be like wow, nuts.
This is not true.
The reason is because someone can actually assess this and that.
Because the person can assess this, he or she can actually modify our code and be able to put in things
that we don't want.
So how do we limit this in order to make sure that only what we want is actually printed out?
Maybe you are in optimization and you have some things that can never be changed.
And if those things cannot be changed, that is when this lecture will be very more interesting to you.
Now, if you want to do that, I will go over to Carlo and I'm going to remove this public.
I'm going to make it to be private.
What?
That is not all.
I'm going to save this right now.
And because I have modified this to black, I remember we have this rainbow with this object is created,
we pass in rainbow right in here.
And this one is the name I made the name of the rainbow to be the acronym of the colors.
Now, if I refresh this, we have a fatal error and what this error said cannot assess private property.
Rambo, you see, that cannot assess private property.
Rambo.
And this error is thrown at line 21.
And that line 21 is this guy here which is trying to modify the color of Rambo.
So in the color of Rambo, you cannot select red, orange, yellow, green, blue, indigo, violet.
So this is where getters and setters play a very vital role.
And now let's quickly dive into setters and getters and learn how it can be able to lock this.
Why we carry out our program and no one can be able to actually alter this.
And as such, anything that is not needed into this.
So let's go ahead and kick off immediately.
So setters actually help us to set the attributes of a class, while guitar helps us to get the attributes
of a class.
So that is what I wanted to have in mind.
So you can write it down and always refer to it.
Always does help you to set the attribute of a class, while guitar helps you to set to get the attribute
of a class.
Why one sets the other one gets it.
Now, because we can be able to access private property of this class, which we have been able to change
to private.
Now, what we are going to do is let's go over here and create a function.
So I'm going to create a function and I'm going to call it out to be get column.
So that is get color and the score head.
And what I'm going to do right in here is to return this because we can no longer get the color down
there.
So return.
And I'm going to use this method This.
So whatever you use this, you go ahead and return the color, which is the color of this private glass
right in the app, because that is what we need.
So go ahead and copy this and paste it here.
And remember to put a semicolon at the end of this.
And now we have to do this.
I'll go right in here and remove this block we put in here.
And that is one out of four we are going to do.
So.
I cannot be able to access this guy here on fresh.
And I still have this arrow right in here.
Now instead of me saying echo removal equal to color, what I'm going to do now is I'm go ahead and
replace this with a get color.
So which is the function we just created up there.
Get color.
All right.
Now go ahead and save this.
And let's go ahead and refresh this.
And you can see we have a right right in the A.
All right.
So that is not all because we are going to set this.
We just well I just want to show you how to use the getter.
Then we go ahead and do the setup because someone can actually still actually assess this board.
Not at all anymore like we did before.
We're going to set it right in there inside this class so that it will be permanent.
And the only the colors you can actually put is either red, orange, yellow, green, blue, indigo
and violet.
So you have to specify these seven colors inside a class function inside a class rainbow, so that it
will always be permanent and nobody will actually go there.
Risk pregnancy.
We have a um, green.
And if we go ahead and save this refresh and have green.
Now what if someone goes in there and say we have black and save this black is there if yesterday's
because of not set the color.
So we need to set it.
But we are currently using the gate because we're actually using the Get method.
Just set the private property of this class.
So now let's go ahead and talk about the gates the set function.
All right.
So now let's go ahead and create another function.
And let that function name be equal to.
Let's call that to be set color.
So that is set color.
And let's go ahead and put a curly brace right in here.
And now this set call is going to take an actual box.
And that is going to be one attribute.
And that is the attribute of color.
So go ahead and copy this and paste in there.
Because we just need only one color right in there.
The are going to be two so equals or we are always going to check between the seven colors of the rainbow.
Ramble.
All right.
Now let's go ahead and see.
We have this right in here.
And what if we go ahead and say this and let this be equal to equal to color.
And then we go ahead and say this is equal to.
According to the color which we just copied in our score here.
And put equal right in here.
And then let's go down here and say that we want to modify that color outside.
Yes, if it still work.
But I'm going to show you step by step the way we're going to lock this.
So I won't show you this.
Then I'll go back and lock it again.
Now, if I should go ahead and, uh, say, uh, color and, uh, let's the color, I mean, return,
uh, Sets.
Sets color function.
Some will call this and I'm going to pass in a parameter.
Then go ahead and say this is equal to.
Equal to red.
And then go ahead and put a semicolon at the end.
And let's go ahead and refresh this and check what it says on the five variable color and line 29.
And uh let's see what happens right in here.
All right.
So now we have been able to set up the gate color and we can actually get the color.
Let's go ahead and set the color and make sure that we lock just the colors of the rainbow right in
there.
So let's go ahead and create a function called set color.
And now this set color is going to take a one attribute.
And that attribute is the color attribute.
So let's go ahead and copy this because this is a private class.
Now let's go ahead and pass in this private column right in here.
So now we have passed in column right in here.
And then the variable color is right in there.
What we need to do now is we have to set this color to the number of the colors we want, so that only
the colors we specify right inside this function will be printed out.
So let's go ahead and see that if this is going to see if and then you go ahead and say if color.
If color is equivalent to then you give the name of the color.
And down here I have red if it is equal to red.
And because I have like about seven colors.
So we're going to use the, all the, all uh operator for this.
So the or operator you go.
We actually have to check and know if we meet this color or this color or this color or under color.
Okay, so if color is equal to red or color is equal to, the next one is orange.
Then okay.
Um.
Next one is orange or the next one is, uh.
Yellow.
Or.
Color is equal to green.
Oh, color.
Is ecotourism.
Let's go ahead and drag this.
I'm gonna shift this a little bit so that I'm able to see what I'm doing.
We have our blue, we have blue.
So I will move this down so that it's kind of consistent from here.
Or color.
Okay I copied that.
So let me go ahead and copy this again so I can actually paste this.
A color is equal to.
Indigo.
How color is equal to.
Violet.
So we have these colors.
We have set up these colors right now.
And I hope that is cool.
So go ahead and set the colors.
And if you set all these colors I'll go ahead and move this down.
So that's a.
Okay.
So now I'll go ahead and put this here.
So if the color is equal to red or color is equal to orange or color is equal to yellow or color is
equal to green or color is equal to blue or color is equal to indigo or violet.
So among all these we're going to see.
This.
And then we go ahead and pass in color.
And I want to see that.
Okay.
So let's go ahead and move this I think there's a mistake.
But let's say color is equal to this color.
So the color is equal to one of these colors because that is, the parameter will pass in there, and
we assign each of these colors to one to them.
So we assign orange to the color variable, and we assign this to the coral variable or coral attributes.
Okay.
Now what if the person puts something that is not the color.
Go ahead and put in else.
Then we can go ahead and print out.
Let's go ahead and see.
This.
This color is equal to invalid.
Invalid color.
Semicolon at the end.
Let's save this.
So this is equal to invalid color.
Now we need to actually go ahead and print this out.
So let's go ahead and check it out.
Now we want to run this.
Where we're going to do is let's go down here and uh, say uh, Rambo.
Let's say Rambo is, uh, I think equal greater than and, uh, sets some set color.
So Rambo, I think rather than set color.
And then we go ahead and, uh, pass in a parameter right here that let's say we have, uh, a block
right in here.
So go ahead and put a semicolon and save this.
And let's go ahead and run this again and check it out.
So invalid color the block we have is the one coming from here which had previously.
We're going to modify this so that this will not be affected again because if we go ahead and uh let
me go ahead and comment on this, let's go ahead and comment on this.
So if we comment on this, on save this and I refresh this, you're going to get block because that
block is still coming from here.
So.
So I can actually come over here and say that the color of a rainbow is white, and it will still work.
You see that?
But if you use this, nothing works.
So the problem is how do we actually lock this so that only these colors will actually be printed out?
Now what I'm going to do is go back to this place where you said this color is equal to this attribute.
Now, what I'm going to do right in here is I will go right here and I'm going to say this set color.
So I'm going to call this function with code here.
I'm going to say this set color.
And then I'm going to pass the color right in there.
All right.
So go ahead and pass the color right in there.
And I'll go ahead and get rid Three of these and I remove this and remember to put your dollar sign
so you don't run into an error.
Go ahead and save this.
So I have modified what we have right in here before.
And then if I actually go over here I'll run this code again.
And let's check what it gives us.
We say this setcolor and we pass in this color right in here.
And, uh, let's go ahead and make sure everything works fine.
Alright, now we have to create the, uh, setup, which is the setcolor.
And, uh, if we go right in here, I'll refresh this.
We still see black.
What if we go over here and then let me say we have our this and I'm going to say rainbow.
And once caught a rainbow and our hyphen grades are done.
And then a golden sea sets color.
And then I'm going to pass in the parameter.
I will say red.
And then at the end and run this code red.
Now go back to this parameter and add some satellite black.
Then set this up.
Run this code and check it out.
Invalid color.
You can see that this sets or the setter is actually working fine.
So we have done this function.
I have done this get function.
And what remains right now is for us to actually look up this so that someone can no longer cheat this.
Because if I should comment on this and go back here and run this code and get some black and that's
black is coming from here, how do we stop people from altering this and make sure that we only print
out the colors that are inside here?
So how can we be able to do that?
That is where the question equation is.
So go to your constructor function and which we have right in here.
So can we able to actually make this to be functioning and look good.
Because that is where what we need to do and make sure that everything works fine.
So go back to constructor and we're going to say this and that.
Instead of column we are going to add in this set color which we actually print here.
So we actually created it.
So I'm going to copy this.
Then I'll go right here.
And I'm going to replace the color with this.
And instead of adding a call to ball color go ahead and delete this.
And now we have this right in here.
So that is cool.
That is what is needed for us to achieve what we are doing.
So you have this here.
You go ahead and refresh this.
And it says undefined variable in line 15 which is the color.
And, uh.
We have color right in here.
And let's go ahead and make sure everything works fine.
So what I actually noticed that when we change the background color, we didn't actually change this
variable because we didn't declare that inside this constructor.
So we can see we still have color right in here.
All right.
So what's actually happening.
And what I'm seeing is that when we changed this, uh, this blue color, we actually remove this as
well because the color was not actually, uh, defined inside this constructor.
So we still have when we deleted this idea.
So we can remove the color and put this to be color, okay.
Because that is actually needed right inside here.
So go ahead and save this.
And now we have save this.
And let's go ahead and refresh fragrance again.
So have invalid color because the black is still here.
So what if I put something like, uh, indigo?
All right, so that is indigo.
And let's go ahead and save this and refresh I will have indigo is printed out.
So if I had put something like, uh Violet and then we save this, we save Violet.
So if someone goes right in there and puts a block again as a color, we can see the black is no longer
accepted, invalid color.
And if someone goes right in here and puts in white and, uh, save this and check it out, you can
see invalid.
So that is how a way you can be able to use getters and setters in your program.
And it looks so beautiful.
So go ahead and check it out.
And if you have any questions, please 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.