WEBVTT

00:00.410 --> 00:09.980
So as we talked about the abstract classes and abstract methods inside those classes, they actually

00:09.980 --> 00:11.360
have to be extended.

00:11.360 --> 00:14.030
So that's the point of an abstract class.

00:14.030 --> 00:19.670
Well, it's actually fine for an abstract class to sit around an extended.

00:19.670 --> 00:21.170
You don't really need to do that.

00:21.170 --> 00:23.570
But then there is no point of such class.

00:23.600 --> 00:33.710
But if you extend you inherit an abstract class, then you need to override those abstract methods and

00:33.710 --> 00:35.210
actually implement them.

00:35.780 --> 00:40.970
Now there is also like another side of the coin to this.

00:41.090 --> 00:48.050
So another spectrum which are final classes and final methods.

00:48.050 --> 00:56.990
And as you can guess, probably these are the classes and methods that you can't extend.

00:57.380 --> 01:01.250
So here we've got an online payment processor.

01:01.280 --> 01:09.800
An abstract class that implements an interface and it is used and it is prepared so that if there is

01:09.800 --> 01:17.270
a need to implement another payment processor except stripe and PayPal, this can be easily done.

01:17.270 --> 01:28.490
But there might be situations when maybe either PayPal or maybe stripe is introducing a new version

01:28.490 --> 01:38.150
of their system, and for some reason, someone on your team thinks it's a good idea to just extend

01:38.150 --> 01:44.450
the stripe processor and maybe call it stripe processor version two.

01:44.450 --> 01:53.210
And maybe this just changes the validate API key or any other behavior.

01:53.210 --> 02:01.130
So anyway, generally someone thinks that creating class like this is a good idea.

02:01.640 --> 02:10.730
So I would advise you against doing that and against having a complex inheritance tree.

02:10.850 --> 02:20.840
I think that inheritance works best only if you use it the way that there is an abstract class that

02:20.840 --> 02:28.580
is there to provide some functionality and show you the way of how something should be done, and then

02:28.580 --> 02:29.540
it is a good idea.

02:29.570 --> 02:37.790
Like this class is a good idea, but then endlessly extending a base class to add more and then add

02:37.820 --> 02:45.020
even more functionality, this can really end up creating a mess.

02:45.020 --> 02:53.690
And if you want to stop people from creating a mess, especially if this might be some junior coworkers,

02:53.840 --> 03:02.220
you just mark a class with the keyword final and if anyone tries this, if they want to extend it,

03:02.220 --> 03:05.160
this is just a fatal error.

03:05.460 --> 03:08.400
You cannot extend a final class.

03:08.670 --> 03:11.610
Now you can also mark methods final.

03:13.110 --> 03:17.970
So let's remove that because this was a bad idea.

03:20.430 --> 03:29.670
Now some people even default to marking the classes as final, and only if you need to extend a class.

03:29.670 --> 03:37.200
You need to then remove the final keyword and possibly also explain why you would like to do that.

03:37.230 --> 03:45.540
Now also, just keep in mind that if you really feel the urge to extend a class that's already there,

03:45.840 --> 03:54.270
just think if maybe composition isn't a better idea, because with every single class, you can actually

03:54.270 --> 03:56.100
pass it through a constructor.

03:56.100 --> 03:59.700
And that is actually a very good pattern.

04:01.170 --> 04:09.390
Now, another useful thing in modern versions of PHP are read only properties.

04:11.040 --> 04:18.570
So currently, for example, with this one we've got the constructor property promotion.

04:20.160 --> 04:27.240
So this is an API key property on this class that can also be accessed in the child classes.

04:27.270 --> 04:34.650
But if the class should be created with a key and then not modified, and you want to make sure the

04:34.650 --> 04:41.970
property is never modified, then you can add a read only keyword to it.

04:41.970 --> 04:50.370
And if anyone attempts to change the value of this property, obviously after it was initialized, this

04:50.370 --> 04:54.150
would also raise a fatal error.

04:54.480 --> 05:03.480
Now, I also think that if you are passing some data to an object, and this data is credentials, keys

05:03.480 --> 05:05.550
or anything that shouldn't change.

05:05.550 --> 05:14.130
You should default to those properties bank read only and only then try to remove read only attribute

05:14.160 --> 05:15.870
read only keyword.

05:16.260 --> 05:21.390
If actually this property should change and that's expected.

05:23.130 --> 05:30.240
Since we are talking about modern PHP features, let's also mention a union type.

05:30.810 --> 05:32.610
This is something like this.

05:34.980 --> 05:36.510
So we are using this.

05:36.540 --> 05:44.640
Let's call it a pipe operator to tell PHP that specific function argument.

05:44.730 --> 05:53.190
This also works with return types and properties that it can take different types, because previously

05:53.220 --> 05:59.970
we were limited to float and if for any reason you've got some data that was an integer.

06:00.240 --> 06:02.370
This just wouldn't work.

06:03.690 --> 06:09.750
So by using this pipe operator you can specify multiple acceptable types.

06:09.750 --> 06:13.410
You can specify as many as you want actually.

06:13.410 --> 06:19.740
But with numbers those two typically can cover most of the cases.

06:20.100 --> 06:25.290
So now after this change we should actually change that everywhere.

06:29.400 --> 06:37.830
Now the union type can also be used for accepting different kinds of values.

06:37.830 --> 06:42.600
By different I mean not just numbers that are float or integer.

06:42.630 --> 06:50.670
But you can add this type to accept maybe an array and also a string.

06:54.900 --> 07:02.230
And to show you an example of that, maybe you can have this process order function that apart from

07:02.230 --> 07:05.800
the amount can also accept items.

07:05.890 --> 07:11.770
So this can be string one item or just an array of items.

07:11.860 --> 07:14.530
So let's call that items.

07:15.790 --> 07:19.120
And here we're going to do some processing with the items.

07:19.120 --> 07:28.300
So after accepting different types of arguments you can also check what is the actual type.

07:28.660 --> 07:31.660
So we can use if is array.

07:31.870 --> 07:35.800
That's the function that will just tell us if that's an array or not.

07:35.860 --> 07:44.260
So if this is an array we can use implode to convert this to a string.

07:44.260 --> 07:47.170
So we use a comma and items.

07:49.630 --> 07:54.370
Otherwise the items list is just a string.

07:54.370 --> 07:56.740
So it is the items.

08:01.780 --> 08:05.770
And here we will always output that.

08:06.250 --> 08:10.390
We are processing order for items.

08:12.910 --> 08:14.950
And we're going to list them.

08:22.030 --> 08:24.670
So maybe this can be a book.

08:24.670 --> 08:31.120
But here we've got an array to be book and movie.

08:31.900 --> 08:35.830
And I said let's have yet another array.

08:36.340 --> 08:39.940
Maybe apple and orange.

08:41.500 --> 08:42.010
Okay.

08:42.010 --> 08:43.690
Let's see if this works.

08:45.460 --> 08:45.760
Okay.

08:45.790 --> 08:47.650
So here are the orders.

08:49.630 --> 08:54.370
So we have just one item and then we've got multiple items.

08:54.370 --> 08:56.200
So everything works fine.
