WEBVTT

00:00.440 --> 00:03.470
I will call you back again to another video lecture in PHP.

00:03.470 --> 00:09.290
And in this video lecture we go ahead and look at getters and setters.

00:16.010 --> 00:19.850
So getters and setters are very important aspects of PHP development.

00:19.850 --> 00:23.210
And we're going to look at it right in here.

00:23.240 --> 00:28.970
Now I have just modified what we did in the previous lecture because we have talked about cars and we

00:28.970 --> 00:31.580
keep on talking about cars is going to be boring.

00:31.580 --> 00:34.850
And some people will be like, why don't you change to something else?

00:34.850 --> 00:42.320
For us to have a better understanding of what objects and classes and object functions and code mean.

00:42.320 --> 00:49.130
So that's what tool getters and setters have to make some modifications by changing this into another

00:49.130 --> 00:54.200
thing altogether, or using every other thing we've learnt in the previous lectures.

00:54.230 --> 00:57.560
Now we're talking about a rainbow right in here.

00:57.560 --> 01:04.790
And we know that rainbow has seven colors, and these colors can never be modified because it is always

01:04.790 --> 01:05.270
there.

01:05.660 --> 01:08.240
Now we have red in here.

01:08.240 --> 01:09.680
I created a statement.

01:09.680 --> 01:18.290
Rainier has seven colors red, orange, yellow, green, blue, indigo, and violet and these are the

01:18.290 --> 01:19.820
only colors that is here.

01:19.850 --> 01:25.010
But as this program is this program, we can actually modify that.

01:25.040 --> 01:26.540
Let's go ahead and refresh this.

01:26.750 --> 01:29.630
We have red color which is red in here.

01:29.660 --> 01:35.780
Now the next thing I'm going to talk about is the getters and setters.

01:36.290 --> 01:40.850
You can see that I'm using var right in here which is the variable I can actually go ahead and change

01:40.850 --> 01:43.040
this to public.

01:43.910 --> 01:51.170
So if we are using public it means that this is now accessible to the public.

01:51.170 --> 01:53.390
And everybody can make changes to here.

01:53.420 --> 01:58.250
So if I go ahead and refresh this we can see that my code is still working fine.

01:58.250 --> 02:04.690
So I've been using VAR in the previous lectures and I can actually use public day, but I left that

02:04.690 --> 02:07.720
so we can talk about it in this lecture.

02:07.780 --> 02:13.480
Now you can see I have public and making this accessible to every kind of person out there.

02:13.480 --> 02:22.000
It means that someone can come back and go over here and tell that the color of this has been modified

02:22.000 --> 02:28.210
by injecting some things into this code, you can say that this is equal to.

02:28.930 --> 02:30.010
It creates another.

02:30.970 --> 02:38.260
The variable symbol is known as hyphen greater than color.

02:38.890 --> 02:46.180
And the person can actually change these and say that we have block in symbol.

02:46.450 --> 02:49.450
And now if we go ahead and run this code check it out.

02:49.450 --> 02:53.110
You can see that we have black and you'll be like wow, nuts.

02:53.110 --> 02:54.550
This is not true.

02:54.580 --> 03:00.580
The reason is because someone can actually assess this and that.

03:00.580 --> 03:08.230
Because the person can assess this, he or she can actually modify our code and be able to put in things

03:08.230 --> 03:09.460
that we don't want.

03:09.490 --> 03:17.590
So how do we limit this in order to make sure that only what we want is actually printed out?

03:17.620 --> 03:22.600
Maybe you are in optimization and you have some things that can never be changed.

03:22.600 --> 03:28.630
And if those things cannot be changed, that is when this lecture will be very more interesting to you.

03:28.660 --> 03:34.600
Now, if you want to do that, I will go over to Carlo and I'm going to remove this public.

03:34.630 --> 03:36.610
I'm going to make it to be private.

03:36.640 --> 03:37.150
What?

03:37.150 --> 03:38.170
That is not all.

03:38.170 --> 03:39.580
I'm going to save this right now.

03:39.580 --> 03:46.270
And because I have modified this to black, I remember we have this rainbow with this object is created,

03:46.270 --> 03:48.310
we pass in rainbow right in here.

03:48.310 --> 03:53.710
And this one is the name I made the name of the rainbow to be the acronym of the colors.

03:53.740 --> 04:01.960
Now, if I refresh this, we have a fatal error and what this error said cannot assess private property.

04:01.990 --> 04:07.720
Rambo, you see, that cannot assess private property.

04:07.750 --> 04:08.320
Rambo.

04:08.320 --> 04:12.340
And this error is thrown at line 21.

04:12.340 --> 04:19.180
And that line 21 is this guy here which is trying to modify the color of Rambo.

04:19.210 --> 04:28.390
So in the color of Rambo, you cannot select red, orange, yellow, green, blue, indigo, violet.

04:28.390 --> 04:32.980
So this is where getters and setters play a very vital role.

04:32.980 --> 04:40.720
And now let's quickly dive into setters and getters and learn how it can be able to lock this.

04:40.720 --> 04:47.680
Why we carry out our program and no one can be able to actually alter this.

04:47.710 --> 04:50.710
And as such, anything that is not needed into this.

04:50.710 --> 04:52.600
So let's go ahead and kick off immediately.

04:53.350 --> 05:01.230
So setters actually help us to set the attributes of a class, while guitar helps us to get the attributes

05:01.230 --> 05:01.980
of a class.

05:01.980 --> 05:04.500
So that is what I wanted to have in mind.

05:04.500 --> 05:08.220
So you can write it down and always refer to it.

05:08.220 --> 05:15.060
Always does help you to set the attribute of a class, while guitar helps you to set to get the attribute

05:15.090 --> 05:15.720
of a class.

05:15.720 --> 05:17.670
Why one sets the other one gets it.

05:17.700 --> 05:25.470
Now, because we can be able to access private property of this class, which we have been able to change

05:25.470 --> 05:26.100
to private.

05:26.130 --> 05:30.390
Now, what we are going to do is let's go over here and create a function.

05:30.570 --> 05:35.580
So I'm going to create a function and I'm going to call it out to be get column.

05:35.580 --> 05:39.540
So that is get color and the score head.

05:39.540 --> 05:48.570
And what I'm going to do right in here is to return this because we can no longer get the color down

05:48.570 --> 05:49.080
there.

05:49.950 --> 05:51.510
So return.

05:51.510 --> 05:56.520
And I'm going to use this method This.

05:56.550 --> 06:05.250
So whatever you use this, you go ahead and return the color, which is the color of this private glass

06:05.250 --> 06:07.320
right in the app, because that is what we need.

06:07.320 --> 06:09.420
So go ahead and copy this and paste it here.

06:09.600 --> 06:12.900
And remember to put a semicolon at the end of this.

06:12.900 --> 06:16.380
And now we have to do this.

06:16.410 --> 06:19.800
I'll go right in here and remove this block we put in here.

06:19.800 --> 06:24.300
And that is one out of four we are going to do.

06:24.300 --> 06:25.080
So.

06:25.080 --> 06:27.540
I cannot be able to access this guy here on fresh.

06:27.540 --> 06:30.060
And I still have this arrow right in here.

06:30.090 --> 06:37.710
Now instead of me saying echo removal equal to color, what I'm going to do now is I'm go ahead and

06:37.890 --> 06:40.290
replace this with a get color.

06:40.290 --> 06:44.430
So which is the function we just created up there.

06:44.640 --> 06:45.810
Get color.

06:49.020 --> 06:49.350
All right.

06:49.380 --> 06:50.820
Now go ahead and save this.

06:50.820 --> 06:52.800
And let's go ahead and refresh this.

06:52.800 --> 06:55.660
And you can see we have a right right in the A.

06:56.170 --> 06:56.740
All right.

06:56.740 --> 07:00.370
So that is not all because we are going to set this.

07:00.400 --> 07:04.450
We just well I just want to show you how to use the getter.

07:04.480 --> 07:10.420
Then we go ahead and do the setup because someone can actually still actually assess this board.

07:10.420 --> 07:13.690
Not at all anymore like we did before.

07:13.720 --> 07:19.570
We're going to set it right in there inside this class so that it will be permanent.

07:19.570 --> 07:26.620
And the only the colors you can actually put is either red, orange, yellow, green, blue, indigo

07:26.620 --> 07:27.250
and violet.

07:27.250 --> 07:33.790
So you have to specify these seven colors inside a class function inside a class rainbow, so that it

07:33.790 --> 07:36.550
will always be permanent and nobody will actually go there.

07:36.610 --> 07:37.510
Risk pregnancy.

07:37.540 --> 07:41.650
We have a um, green.

07:41.650 --> 07:45.070
And if we go ahead and save this refresh and have green.

07:45.070 --> 07:54.660
Now what if someone goes in there and say we have black and save this black is there if yesterday's

07:54.660 --> 07:56.160
because of not set the color.

07:56.160 --> 07:57.120
So we need to set it.

07:57.120 --> 08:01.530
But we are currently using the gate because we're actually using the Get method.

08:01.560 --> 08:04.770
Just set the private property of this class.

08:05.160 --> 08:11.970
So now let's go ahead and talk about the gates the set function.

08:12.000 --> 08:12.750
All right.

08:13.380 --> 08:16.260
So now let's go ahead and create another function.

08:16.260 --> 08:20.760
And let that function name be equal to.

08:21.090 --> 08:25.620
Let's call that to be set color.

08:26.640 --> 08:28.200
So that is set color.

08:28.200 --> 08:31.950
And let's go ahead and put a curly brace right in here.

08:31.950 --> 08:34.740
And now this set call is going to take an actual box.

08:34.740 --> 08:36.420
And that is going to be one attribute.

08:36.420 --> 08:38.970
And that is the attribute of color.

08:38.970 --> 08:43.170
So go ahead and copy this and paste in there.

08:43.200 --> 08:45.870
Because we just need only one color right in there.

08:45.900 --> 08:52.260
The are going to be two so equals or we are always going to check between the seven colors of the rainbow.

08:52.260 --> 08:52.280
Ramble.

08:52.310 --> 08:52.820
All right.

08:52.850 --> 08:54.140
Now let's go ahead and see.

08:54.170 --> 08:55.550
We have this right in here.

08:55.550 --> 09:06.980
And what if we go ahead and say this and let this be equal to equal to color.

09:06.980 --> 09:13.700
And then we go ahead and say this is equal to.

09:15.770 --> 09:19.040
According to the color which we just copied in our score here.

09:19.040 --> 09:20.990
And put equal right in here.

09:20.990 --> 09:26.870
And then let's go down here and say that we want to modify that color outside.

09:26.870 --> 09:28.220
Yes, if it still work.

09:28.220 --> 09:31.580
But I'm going to show you step by step the way we're going to lock this.

09:31.580 --> 09:32.990
So I won't show you this.

09:33.020 --> 09:34.970
Then I'll go back and lock it again.

09:35.000 --> 09:48.080
Now, if I should go ahead and, uh, say, uh, color and, uh, let's the color, I mean, return,

09:48.410 --> 09:51.680
uh, Sets.

09:52.430 --> 09:53.750
Sets color function.

09:53.750 --> 09:58.280
Some will call this and I'm going to pass in a parameter.

09:58.310 --> 10:01.700
Then go ahead and say this is equal to.

10:04.010 --> 10:05.390
Equal to red.

10:05.540 --> 10:07.940
And then go ahead and put a semicolon at the end.

10:07.940 --> 10:16.370
And let's go ahead and refresh this and check what it says on the five variable color and line 29.

10:16.640 --> 10:20.540
And uh let's see what happens right in here.

10:23.000 --> 10:23.420
All right.

10:23.450 --> 10:29.930
So now we have been able to set up the gate color and we can actually get the color.

10:29.960 --> 10:35.480
Let's go ahead and set the color and make sure that we lock just the colors of the rainbow right in

10:35.510 --> 10:35.750
there.

10:35.750 --> 10:40.550
So let's go ahead and create a function called set color.

10:41.690 --> 10:45.920
And now this set color is going to take a one attribute.

10:45.920 --> 10:48.770
And that attribute is the color attribute.

10:48.770 --> 10:53.270
So let's go ahead and copy this because this is a private class.

10:53.300 --> 10:58.490
Now let's go ahead and pass in this private column right in here.

10:58.490 --> 11:01.910
So now we have passed in column right in here.

11:01.910 --> 11:04.310
And then the variable color is right in there.

11:04.340 --> 11:10.040
What we need to do now is we have to set this color to the number of the colors we want, so that only

11:10.040 --> 11:13.790
the colors we specify right inside this function will be printed out.

11:13.790 --> 11:23.570
So let's go ahead and see that if this is going to see if and then you go ahead and say if color.

11:25.520 --> 11:29.870
If color is equivalent to then you give the name of the color.

11:29.870 --> 11:35.120
And down here I have red if it is equal to red.

11:35.150 --> 11:37.280
And because I have like about seven colors.

11:37.280 --> 11:41.840
So we're going to use the, all the, all uh operator for this.

11:41.840 --> 11:43.310
So the or operator you go.

11:43.340 --> 11:50.410
We actually have to check and know if we meet this color or this color or this color or under color.

11:50.440 --> 12:01.300
Okay, so if color is equal to red or color is equal to, the next one is orange.

12:03.580 --> 12:06.820
Then okay.

12:06.850 --> 12:07.390
Um.

12:09.790 --> 12:17.200
Next one is orange or the next one is, uh.

12:19.450 --> 12:20.050
Yellow.

12:24.880 --> 12:25.510
Or.

12:28.030 --> 12:32.980
Color is equal to green.

12:40.030 --> 12:41.230
Oh, color.

12:44.590 --> 12:46.060
Is ecotourism.

12:46.090 --> 12:47.140
Let's go ahead and drag this.

12:47.170 --> 12:51.070
I'm gonna shift this a little bit so that I'm able to see what I'm doing.

12:51.100 --> 12:55.120
We have our blue, we have blue.

13:00.910 --> 13:04.750
So I will move this down so that it's kind of consistent from here.

13:07.810 --> 13:08.710
Or color.

13:12.880 --> 13:14.200
Okay I copied that.

13:14.200 --> 13:19.300
So let me go ahead and copy this again so I can actually paste this.

13:24.280 --> 13:26.560
A color is equal to.

13:28.750 --> 13:29.830
Indigo.

13:36.130 --> 13:38.680
How color is equal to.

13:41.710 --> 13:42.460
Violet.

13:42.460 --> 13:44.470
So we have these colors.

13:44.470 --> 13:47.050
We have set up these colors right now.

13:47.050 --> 13:48.850
And I hope that is cool.

13:48.850 --> 13:51.310
So go ahead and set the colors.

13:51.310 --> 13:55.840
And if you set all these colors I'll go ahead and move this down.

13:55.840 --> 13:56.770
So that's a.

13:59.350 --> 13:59.950
Okay.

13:59.950 --> 14:04.480
So now I'll go ahead and put this here.

14:04.480 --> 14:10.000
So if the color is equal to red or color is equal to orange or color is equal to yellow or color is

14:10.000 --> 14:15.010
equal to green or color is equal to blue or color is equal to indigo or violet.

14:15.010 --> 14:19.090
So among all these we're going to see.

14:21.250 --> 14:22.390
This.

14:23.590 --> 14:27.850
And then we go ahead and pass in color.

14:28.300 --> 14:30.040
And I want to see that.

14:32.830 --> 14:33.280
Okay.

14:33.310 --> 14:35.800
So let's go ahead and move this I think there's a mistake.

14:35.800 --> 14:40.810
But let's say color is equal to this color.

14:41.380 --> 14:49.050
So the color is equal to one of these colors because that is, the parameter will pass in there, and

14:49.080 --> 14:52.830
we assign each of these colors to one to them.

14:52.830 --> 15:01.530
So we assign orange to the color variable, and we assign this to the coral variable or coral attributes.

15:01.560 --> 15:02.190
Okay.

15:02.220 --> 15:07.620
Now what if the person puts something that is not the color.

15:07.650 --> 15:10.500
Go ahead and put in else.

15:10.530 --> 15:13.860
Then we can go ahead and print out.

15:14.250 --> 15:15.840
Let's go ahead and see.

15:18.000 --> 15:19.050
This.

15:21.390 --> 15:28.230
This color is equal to invalid.

15:31.200 --> 15:32.130
Invalid color.

15:32.730 --> 15:34.020
Semicolon at the end.

15:34.050 --> 15:35.340
Let's save this.

15:36.030 --> 15:38.580
So this is equal to invalid color.

15:38.610 --> 15:43.470
Now we need to actually go ahead and print this out.

15:44.430 --> 15:46.980
So let's go ahead and check it out.

15:47.310 --> 15:49.320
Now we want to run this.

15:49.320 --> 15:54.750
Where we're going to do is let's go down here and uh, say uh, Rambo.

15:57.000 --> 16:05.580
Let's say Rambo is, uh, I think equal greater than and, uh, sets some set color.

16:06.420 --> 16:10.110
So Rambo, I think rather than set color.

16:10.110 --> 16:18.810
And then we go ahead and, uh, pass in a parameter right here that let's say we have, uh, a block

16:18.810 --> 16:19.470
right in here.

16:19.470 --> 16:22.020
So go ahead and put a semicolon and save this.

16:22.020 --> 16:24.180
And let's go ahead and run this again and check it out.

16:24.180 --> 16:29.490
So invalid color the block we have is the one coming from here which had previously.

16:29.520 --> 16:36.660
We're going to modify this so that this will not be affected again because if we go ahead and uh let

16:36.660 --> 16:39.240
me go ahead and comment on this, let's go ahead and comment on this.

16:39.240 --> 16:44.100
So if we comment on this, on save this and I refresh this, you're going to get block because that

16:44.100 --> 16:45.270
block is still coming from here.

16:45.300 --> 16:45.780
So.

16:45.780 --> 16:53.250
So I can actually come over here and say that the color of a rainbow is white, and it will still work.

16:53.280 --> 16:54.030
You see that?

16:54.060 --> 16:56.460
But if you use this, nothing works.

16:56.460 --> 17:04.500
So the problem is how do we actually lock this so that only these colors will actually be printed out?

17:04.530 --> 17:12.720
Now what I'm going to do is go back to this place where you said this color is equal to this attribute.

17:12.750 --> 17:21.570
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.

17:21.600 --> 17:27.030
So I'm going to call this function with code here.

17:27.060 --> 17:30.060
I'm going to say this set color.

17:30.060 --> 17:34.260
And then I'm going to pass the color right in there.

17:35.520 --> 17:35.970
All right.

17:35.970 --> 17:40.050
So go ahead and pass the color right in there.

17:40.050 --> 17:53.400
And I'll go ahead and get rid Three of these and I remove this and remember to put your dollar sign

17:53.400 --> 17:55.080
so you don't run into an error.

17:55.110 --> 17:56.010
Go ahead and save this.

17:56.010 --> 17:58.680
So I have modified what we have right in here before.

17:58.680 --> 18:02.220
And then if I actually go over here I'll run this code again.

18:02.220 --> 18:06.780
And let's check what it gives us.

18:06.810 --> 18:12.900
We say this setcolor and we pass in this color right in here.

18:13.050 --> 18:18.060
And, uh, let's go ahead and make sure everything works fine.

18:18.600 --> 18:23.550
Alright, now we have to create the, uh, setup, which is the setcolor.

18:23.550 --> 18:26.910
And, uh, if we go right in here, I'll refresh this.

18:26.940 --> 18:28.050
We still see black.

18:28.050 --> 18:36.630
What if we go over here and then let me say we have our this and I'm going to say rainbow.

18:37.650 --> 18:42.120
And once caught a rainbow and our hyphen grades are done.

18:42.120 --> 18:47.000
And then a golden sea sets color.

18:47.150 --> 18:50.000
And then I'm going to pass in the parameter.

18:50.030 --> 18:51.920
I will say red.

18:51.920 --> 18:56.300
And then at the end and run this code red.

18:56.330 --> 19:00.680
Now go back to this parameter and add some satellite black.

19:02.900 --> 19:03.980
Then set this up.

19:03.980 --> 19:05.330
Run this code and check it out.

19:05.360 --> 19:06.440
Invalid color.

19:08.060 --> 19:12.560
You can see that this sets or the setter is actually working fine.

19:12.710 --> 19:14.750
So we have done this function.

19:14.780 --> 19:16.160
I have done this get function.

19:16.160 --> 19:22.490
And what remains right now is for us to actually look up this so that someone can no longer cheat this.

19:22.490 --> 19:29.150
Because if I should comment on this and go back here and run this code and get some black and that's

19:29.150 --> 19:36.020
black is coming from here, how do we stop people from altering this and make sure that we only print

19:36.050 --> 19:37.760
out the colors that are inside here?

19:38.870 --> 19:40.520
So how can we be able to do that?

19:40.520 --> 19:41.990
That is where the question equation is.

19:41.990 --> 19:45.980
So go to your constructor function and which we have right in here.

19:46.010 --> 19:51.950
So can we able to actually make this to be functioning and look good.

19:51.980 --> 19:57.260
Because that is where what we need to do and make sure that everything works fine.

19:57.260 --> 20:02.060
So go back to constructor and we're going to say this and that.

20:02.060 --> 20:08.570
Instead of column we are going to add in this set color which we actually print here.

20:08.600 --> 20:10.430
So we actually created it.

20:10.430 --> 20:11.780
So I'm going to copy this.

20:11.810 --> 20:13.940
Then I'll go right here.

20:14.060 --> 20:17.090
And I'm going to replace the color with this.

20:17.090 --> 20:22.520
And instead of adding a call to ball color go ahead and delete this.

20:22.700 --> 20:25.610
And now we have this right in here.

20:25.850 --> 20:27.050
So that is cool.

20:27.920 --> 20:32.150
That is what is needed for us to achieve what we are doing.

20:32.150 --> 20:33.440
So you have this here.

20:33.440 --> 20:35.630
You go ahead and refresh this.

20:35.630 --> 20:42.770
And it says undefined variable in line 15 which is the color.

20:42.860 --> 20:44.030
And, uh.

20:46.160 --> 20:48.950
We have color right in here.

20:49.070 --> 20:52.430
And let's go ahead and make sure everything works fine.

20:53.930 --> 20:59.420
So what I actually noticed that when we change the background color, we didn't actually change this

20:59.420 --> 21:02.720
variable because we didn't declare that inside this constructor.

21:02.720 --> 21:05.000
So we can see we still have color right in here.

21:07.520 --> 21:08.030
All right.

21:08.030 --> 21:09.260
So what's actually happening.

21:09.260 --> 21:16.640
And what I'm seeing is that when we changed this, uh, this blue color, we actually remove this as

21:16.640 --> 21:23.690
well because the color was not actually, uh, defined inside this constructor.

21:23.690 --> 21:26.150
So we still have when we deleted this idea.

21:26.180 --> 21:30.170
So we can remove the color and put this to be color, okay.

21:30.170 --> 21:33.290
Because that is actually needed right inside here.

21:33.290 --> 21:36.740
So go ahead and save this.

21:36.770 --> 21:38.570
And now we have save this.

21:38.570 --> 21:40.420
And let's go ahead and refresh fragrance again.

21:40.420 --> 21:43.480
So have invalid color because the black is still here.

21:43.480 --> 21:47.200
So what if I put something like, uh, indigo?

21:49.360 --> 21:51.280
All right, so that is indigo.

21:51.310 --> 21:55.630
And let's go ahead and save this and refresh I will have indigo is printed out.

21:55.630 --> 22:03.820
So if I had put something like, uh Violet and then we save this, we save Violet.

22:03.820 --> 22:10.450
So if someone goes right in there and puts a block again as a color, we can see the black is no longer

22:10.450 --> 22:12.610
accepted, invalid color.

22:12.610 --> 22:20.680
And if someone goes right in here and puts in white and, uh, save this and check it out, you can

22:20.680 --> 22:21.970
see invalid.

22:21.970 --> 22:26.770
So that is how a way you can be able to use getters and setters in your program.

22:26.770 --> 22:28.840
And it looks so beautiful.

22:28.840 --> 22:30.160
So go ahead and check it out.

22:30.160 --> 22:33.640
And if you have any questions, please use the question and answer section.

22:33.640 --> 22:35.830
And I'm going to get back to you as soon as possible.

22:35.830 --> 22:36.700
Thank you so much.

22:36.700 --> 22:39.610
And I'm going to see you in the next video lecture.
