WEBVTT

00:00.140 --> 00:05.990
So now we are going to learn about the interfaces and abstract classes.

00:06.020 --> 00:08.930
So starting with interfaces.

00:09.170 --> 00:15.860
So interfaces just define a contract that certain classes have to sign.

00:15.920 --> 00:20.990
And this just guarantees that those classes will have certain functionality.

00:21.020 --> 00:25.520
Specifically they will implement some methods.

00:25.520 --> 00:29.690
Interfaces themselves don't implement anything.

00:29.720 --> 00:32.660
They just define the methods.

00:33.170 --> 00:36.680
So let's start by defining an interface.

00:36.680 --> 00:42.050
And as an example here we are going to work with payment processors.

00:42.050 --> 00:48.320
And the goal is to have an interface that defines some of the behaviors.

00:48.320 --> 00:56.630
And then the classes that might implement PayPal payment processor or stripe payment processor.

00:57.080 --> 01:06.270
And by doing that we basically allow our app Up to easily swap between different payment processors

01:06.300 --> 01:09.570
without making any changes to the code.

01:11.040 --> 01:15.600
So the interface is defined by using the interface keyword.

01:15.930 --> 01:20.010
It needs to be given a name so we can go with just payment.

01:20.010 --> 01:28.290
Processor and interfaces define methods but never implement them.

01:30.450 --> 01:33.450
So I'm going to go with public function.

01:33.720 --> 01:37.830
Process payment.

01:40.020 --> 01:45.990
Which needs to accept an amount and returns a boolean on success.

01:46.590 --> 01:49.980
Notice how I'm using a semicolon here.

01:49.980 --> 01:55.110
As as I've said, there is no implementation in the interface.

01:57.060 --> 02:00.240
Let's also have another method here.

02:01.470 --> 02:05.610
So this would be refund payment.

02:06.750 --> 02:14.160
So signing a contract by a class means that a class implements such interface.

02:14.160 --> 02:22.680
And thus PHP will make sure that the given class has this method with exactly this header, and also

02:22.710 --> 02:25.680
that it has this second method.

02:25.710 --> 02:28.890
Otherwise this would be a fatal error.

02:29.460 --> 02:29.940
Okay.

02:29.970 --> 02:33.840
Now let's proceed with the implementations.

02:35.850 --> 02:36.030
Okay.

02:36.060 --> 02:41.250
So I think we should see an example of a class that implements the interface.

02:42.210 --> 02:45.720
So let's create a stripe processor.

02:45.750 --> 02:50.820
So if you don't know stripe this is an online payment processor.

02:50.850 --> 02:54.090
Essentially it lets you accept payments.

02:54.870 --> 03:00.690
And to implement an interface you're going to use the implements keyword.

03:00.690 --> 03:06.340
And just name the interface Dad's payment processor.

03:07.480 --> 03:15.700
Now, with this code here, we expect to see a fatal error because as I've mentioned before, when a

03:15.700 --> 03:24.040
class implements the interface, it actually needs to implement all the methods from the interface.

03:25.090 --> 03:26.530
Let's run this.

03:26.950 --> 03:35.050
And there's the fatal error saying something about abstract methods or implementing them vars.

03:35.620 --> 03:44.020
Next up, let's jump to the concept of abstract classes and abstract methods.

03:45.580 --> 03:47.500
So currently we have a choice.

03:47.530 --> 03:56.620
We either implement all the methods from the interface in this stripe processor class, or we mark it

03:56.620 --> 03:58.030
as abstract.

03:58.720 --> 04:03.730
Now making a class abstract has some serious implications.

04:03.730 --> 04:08.750
The main one is that you can't create an instance of such class.

04:09.860 --> 04:17.990
So if I'd like to go and do new stripe processor, this will just throw a fatal error.

04:18.020 --> 04:21.230
You can't create an abstract classes.

04:21.350 --> 04:26.120
So every method in a class needs to be implemented.

04:26.240 --> 04:29.660
So what's the point of the abstract classes.

04:30.950 --> 04:39.980
So interfaces lets you make sure that some classes have the specific functionality that is required.

04:40.220 --> 04:47.330
So by implementing that interface we just make sure that a class has those two possibilities that it

04:47.330 --> 04:52.010
can process payments and that it can refund payments.

04:52.040 --> 04:55.520
And how does it do that is not our concern.

04:55.790 --> 05:03.830
Now when you have abstract classes, what it lets you do is you might want to implement some common

05:04.090 --> 05:09.700
Nationality across all of your payment processors.

05:10.810 --> 05:18.250
So if I have a stripe processor, but I might also have a PayPal processor.

05:18.280 --> 05:20.920
This makes perfect sense.

05:21.370 --> 05:26.620
And there might be some common functionality across those two classes.

05:26.620 --> 05:32.680
Like they both take an API key to connect to those services.

05:33.520 --> 05:39.490
Maybe they can have some common logic that is required by your app.

05:39.490 --> 05:49.390
And then creating an abstract class which can implement this functionality is actually a great idea.

05:49.540 --> 06:02.680
So let's create an abstract class that we're going to call online payment Processor that will implement

06:03.190 --> 06:04.810
our interface.

06:04.810 --> 06:06.750
That's payment.

06:08.940 --> 06:09.990
Processor.

06:13.440 --> 06:16.050
So now let's make those two classes.

06:16.050 --> 06:17.820
Concrete classes.

06:18.150 --> 06:22.440
A concrete class is a class that's not abstract.

06:22.650 --> 06:27.150
It means that it implements all the interface methods.

06:27.150 --> 06:31.200
And it also implements all the abstract methods.

06:31.890 --> 06:35.610
So it won't directly implement the interface.

06:35.610 --> 06:43.500
Instead this would extend the online payment processor.

06:43.530 --> 06:51.240
And by extending the online payment processor it actually also implements that interface.

06:51.240 --> 06:54.300
So this is all inherited.

06:54.330 --> 07:00.360
Now this won't work yet because still this is an abstract class.

07:00.360 --> 07:05.580
And the methods from the interface are not implemented anywhere.

07:06.340 --> 07:12.910
But since we have this abstract class, let's add something that will justify its existence, which

07:12.910 --> 07:18.220
is some common functionality that those two concrete classes should share.

07:19.450 --> 07:27.310
So let's assume that both of those payment processors accept a kind of an API key that's just used to

07:27.340 --> 07:28.660
connect to them.

07:29.350 --> 07:36.220
So I'm going to create a constructor here that will essentially define a field.

07:37.420 --> 07:43.450
Now I'm using the protected visibility keyword and this is API key.

07:44.050 --> 07:46.240
Now we haven't used protected before.

07:46.240 --> 07:47.800
So let me explain this.

07:47.830 --> 07:54.340
So public means you can access methods and fields inside and outside the class.

07:54.460 --> 07:57.040
This is the public interface to your class.

07:57.040 --> 08:01.270
This is what others using this class can actually call.

08:02.410 --> 08:03.580
And private.

08:03.580 --> 08:11.000
As we might remember, are the fields and methods that are only accessible inside a specific class.

08:11.180 --> 08:18.650
And the difference between private and protected is that protected fields and methods are not accessible

08:18.650 --> 08:25.400
outside the class, but they are accessible in classes that extend them.

08:25.610 --> 08:33.950
So this API key field can be accessed in those classes inside those classes that extend this base class.

08:34.880 --> 08:42.710
And if you have private fields or private methods, those cannot be accessed outside the class, but

08:42.710 --> 08:47.300
also can't be accessed in classes that extend the base class.

08:47.330 --> 08:48.890
So that's the difference.

08:51.140 --> 08:51.410
Okay.

08:51.410 --> 08:58.010
So now both of those payment processors, they get access to the API key.

08:58.580 --> 09:04.160
Now there might be some different requirements for the API key in both of those.

09:04.370 --> 09:07.010
So we're going to go back to this later on.

09:07.040 --> 09:15.210
For now I'd like this abstract class to implement the methods from the interface, because we'd actually

09:15.210 --> 09:19.470
like to have the logic that is kind of the same.

09:19.470 --> 09:24.660
And in both cases, we'd like to verify if the API key is correct.

09:24.660 --> 09:29.670
So now I'm going to implement those methods from the interface.

09:29.790 --> 09:38.220
The easiest way to start is just to copy those like that and just add the implementation.

09:40.890 --> 09:48.750
Now by the way, even though those methods are empty, we can actually create those classes now because

09:48.750 --> 09:51.330
the methods are now implemented.

09:52.410 --> 10:00.120
So if I'm going to do processor and I'm going to pass some kind of a key, then when I run this code.

10:02.430 --> 10:03.750
Everything is fine.

10:05.640 --> 10:06.000
All right.

10:06.000 --> 10:09.150
So at this point I'd like to take a break.

10:09.180 --> 10:11.670
Let's summarize what we've learned so far.

10:12.150 --> 10:19.800
So we can define an interface, which basically is like signing a contract by a class that it guarantees

10:19.920 --> 10:22.050
to have this functionality.

10:22.080 --> 10:25.740
It has to implement all the methods from the interface.

10:26.490 --> 10:33.390
Then there are abstract classes, and the abstract class is a class that has at least one abstract method.

10:33.480 --> 10:38.670
If you don't implement a method from an interface, it's automatically abstract.

10:38.700 --> 10:45.450
You can also explicitly mark methods as abstract, but we're gonna see that in the next example.

10:45.450 --> 10:49.080
So we've created this abstract class that is not concrete.

10:49.080 --> 10:53.190
So it cannot be created and it implements the interface.

10:53.190 --> 11:01.650
Now we want to have the common functionality of those two methods, but also with some specifics that

11:01.650 --> 11:05.610
will be implemented in those two concrete classes.

11:06.870 --> 11:09.720
So we're going to go back to it after a short break.
