WEBVTT

00:00.650 --> 00:06.830
So next up we are going to learn about the inheritance and polymorphism.

00:08.180 --> 00:10.730
So we've got a person class already.

00:11.300 --> 00:16.880
Now let's create another class that would be based on this person class.

00:16.880 --> 00:18.830
This is called inheritance.

00:18.980 --> 00:23.090
I am defining a class that will be called employee.

00:23.660 --> 00:31.820
I use the keyword extends to tell PHP that this class is based on person.

00:32.450 --> 00:41.360
So that's inheritance because it inherits all the fields and all the methods of the parent class, but

00:41.360 --> 00:43.940
it can add something on its own.

00:44.210 --> 00:49.580
Now let me copy the constructor from the person to the employee.

00:51.110 --> 00:56.150
And we are doing this because I'd like to add another fields in here.

00:56.150 --> 01:00.230
So this would be a string called position.

01:01.730 --> 01:05.330
So employee has position at work.

01:05.960 --> 01:13.760
Okay, now I'd like the message for the employee to be a little different than the one from person.

01:13.790 --> 01:21.200
Additionally, when introducing an employee, I'd like to know what's the position of an employee?

01:21.260 --> 01:30.080
Now I can create an employee right now by using new employee.

01:30.110 --> 01:32.180
I can pass the name.

01:33.950 --> 01:39.860
I can pass the age and I can pass the position.

01:39.920 --> 01:43.700
Let's say that that's a manager.

01:45.440 --> 01:51.110
Now, when I echo the employee introduce method.

01:52.220 --> 01:54.620
This should all work fine.

01:55.520 --> 01:58.010
So I'm Jerry and I'm 45 years old.

01:58.040 --> 02:02.150
This is matching the data that we've passed to the employee class.

02:02.780 --> 02:10.970
So the things that we should learn in here is that the employee has all the data and all the methods

02:10.970 --> 02:12.530
of the base class.

02:12.530 --> 02:14.720
So person is the base class.

02:14.750 --> 02:18.110
This method is only defined in the person class.

02:18.110 --> 02:25.310
Yet when we create an employee which extends person, we can call this method.

02:27.800 --> 02:35.210
Now in our case, using this introduce method from the base class is not ideal because the base class

02:35.240 --> 02:39.590
does not know about the position, it's only about the person.

02:39.590 --> 02:45.680
So when introducing an employee, it's still only going to mention the name and the age.

02:46.340 --> 02:50.630
So what we can do is we can overwrite this method.

02:50.630 --> 02:51.500
That's important.

02:51.500 --> 02:53.240
Word overriding.

02:53.810 --> 03:01.850
Overriding means we redefine the method that's specific for an employee.

03:03.140 --> 03:13.860
And now I can say I work as a this position.

03:16.440 --> 03:17.190
Dot.

03:18.120 --> 03:18.960
Now.

03:21.570 --> 03:24.990
Let's see how the message will look like right now.

03:24.990 --> 03:29.130
By the way, let me concatenate that with a new line.

03:29.790 --> 03:31.680
Okay, let's run this.

03:32.130 --> 03:34.590
I work as a manager.

03:34.770 --> 03:35.190
Mm.

03:35.730 --> 03:39.750
So we've got the information that we wanted to convey.

03:39.750 --> 03:44.910
We state the position, but we are losing the previous behavior.

03:44.910 --> 03:52.410
So it might look like we are actually doing something that doesn't really benefit us.

03:52.440 --> 03:54.870
And to fix this.

04:01.560 --> 04:11.160
We just need to make sure we are also using the base class method and to access inside the classes the

04:11.160 --> 04:14.310
methods or data from the parent class.

04:14.340 --> 04:23.400
We use the special parent syntax double colon, and that way I can call the introduce method from the

04:23.400 --> 04:26.580
parent, which should just output the text.

04:28.800 --> 04:31.260
That introduces the person.

04:31.740 --> 04:38.580
And I can add a space here so we can concatenate that with our own more specific implementation.

04:38.580 --> 04:46.710
So now when I run this we've got the base introduction plus the position.

04:49.380 --> 04:49.650
Okay.

04:49.650 --> 04:53.460
So now let's see an example of polymorphism.

04:54.150 --> 04:56.790
Instead of just one variable.

04:57.240 --> 05:01.020
Let's define an array of people.

05:03.180 --> 05:07.830
Now the first element in this array would be our employee Jerry.

05:09.090 --> 05:14.400
But we're also going to have a person, just a person that doesn't work anywhere.

05:14.970 --> 05:15.930
That would be me.

05:18.300 --> 05:21.480
So we can still use this person class.

05:21.960 --> 05:33.060
Now let me define a function that will be called introduce and it accepts an argument of type person.

05:35.640 --> 05:42.660
Now, thanks to polymorphism, when we have a common parent class, we can treat objects of different

05:42.660 --> 05:46.620
classes as if they are the same parent class.

05:46.860 --> 06:00.060
So in case of this simple method, I can do echo person introduce and I concatenate that with a new

06:00.060 --> 06:00.810
line.

06:00.840 --> 06:08.640
Now let's also change the name of this variable to people not employees because it contains different

06:08.650 --> 06:12.460
classes, but they all have the same base class.

06:12.490 --> 06:16.600
Now, in the script I'm just going to do for each.

06:18.610 --> 06:21.550
People as person.

06:22.510 --> 06:29.230
And I will just call the introduce function, which accepts an object of class person.

06:29.230 --> 06:37.480
And with polymorphism, actually both the employee and person can be treated as if they are the person

06:37.480 --> 06:45.790
class, because the object which is of class person is actually of this specific class, and the employee

06:45.820 --> 06:50.410
extends the person, which means it will have the introduce method.

06:50.410 --> 06:51.490
For example.

06:51.490 --> 06:54.970
So I can call introduce passing the person.

06:54.970 --> 06:56.080
Now let's run this.

06:56.080 --> 07:02.350
And those objects have their specific implementation of the introduce method with Jerry.

07:02.380 --> 07:05.410
This will add his position.

07:05.470 --> 07:10.270
Now with me it's only saying that I'm pure and I'm 37 Seven.

07:11.230 --> 07:17.470
Now, at this point, I'd like you to appreciate how important polymorphism is.

07:17.710 --> 07:25.780
Now, what you see here is we've created a function that accepts an argument, and you've just seen

07:25.780 --> 07:32.290
us passing objects of different classes, yet everything has worked perfectly.

07:32.290 --> 07:39.190
And the same method could be run for different objects, yet it yielded different results.

07:39.190 --> 07:47.500
So that is the power of polymorphism that you can create code that doesn't have to change if some specific

07:47.500 --> 07:50.890
implementation on your classes changes.

07:51.580 --> 07:57.190
So we just have a base class which has some base methods and we can depend on that.

07:57.190 --> 08:06.040
And if we then decide to add more classes like a boss class or a director class, this code does not

08:06.040 --> 08:06.760
have to change.

08:06.760 --> 08:11.890
It will still function and it will still properly introduce everyone.
