Playing
01. So, what's an interface?

Episodes

0%
Your progress
  • Total: 8m
  • Played: 0m
  • Remaining: 8m
Join or sign in to track your progress

Transcript

00:00
Within programming an interface is actually a very simple concept, so in this video we're going to take a look at exactly what an interface is within PHP, we're going to go ahead and write one and we're going to create a couple of classes that use that interface.
00:16
So really to start explaining we're going to head over to our text area and actually start to create an interface. So we're going to be building the ability to store something somewhere. So we're going to build a class that allows you to store a particular value under a particular
00:33
key and then also then retrieve that value back. So pretty simple, this could be in a session, it could be in a database, it could be in a file, it could be in a cookie, whatever, it doesn't matter and that's the point of using an interface or coding to an interface.
00:50
So I have an app directory open just here, I'm going to go ahead and create a new file in here and we're going to call this storage interface like so. So within here then let's just give this a namespace, this doesn't really matter and we're going to use the interface keyword.
01:12
So when we create an interface and this is usually the first thing that we do, we basically specify what methods we want to implement within a class. So let's go ahead and do this because this can seem a little bit confusing at first. Here's my interface.
01:29
Now all I'm going to do is I'm going to say, well, what do I need to be able to put things into storage and then take things out of storage? Well I need two methods really and of course there could be more but I need the ability to set a value and would give a key to store that under and would give the value we want
01:51
to store and then we would need a get method in order to pull that value out. And notice here I've not actually defined the method, I've just created the method outline. So this on its own is useless. We can't do anything with that.
02:08
We can't create a new storage instance, storage interface instance because we don't have any functionality in here. But now what we can actually do is we can create a session storage, we can create a database storage, a cookie storage, whatever.
02:23
So let's go ahead and create a session storage then we'll use it and then when we switch over to use another method of storage, you'll see how useful an interface can be. So I'm going to call this session store.php. Let's go and give this our namespace, again this doesn't really matter.
02:41
And let's call this session store. So this is a class now. Now what we want to do is as a developer, say this was someone else's project and we say, well, we want to store things in the session.
02:53
We're going to implement the storage interface and that should be implement. So now if we go ahead and use this session storage class, so let's do this in here, we'll just require in vendor autoload, I'm just autoloading these files in. So I'm going to say s equals new session store and we can just use app session store
03:20
at the top. So just coming over to my browser, you can see here as we've got store interface and I believe it's storage interface. So let's correct that and there we go.
03:31
So now we have an error that tells us that the session store contains two abstract methods and therefore must be declared abstract or implement the remaining methods. All this means is that because we have implemented this contract, we know that our session store has to have the ability to set something in a session and then get something out of the
03:55
session. So we can just go ahead and define these. So let's say public function set, we know we give a key here and we give a value and we have another method here to get.
04:09
So we've now completed the class, we've adhered to this contract, this storage interface contract, two things that we need. So let's just very quickly implement this functionality. So to set, we're just going to say session key equals value and then to get something
04:28
out of a session, we're going to return dollar underscore session key. So two very simple methods but obviously this is just an example. So over on the index page then, let's say S set and let's set the name here to Mo and now let's echo S get name.
04:52
So here we're obviously just setting the value Mo into the key name within a session and we shouldn't really forget to start sessions up here and then we're just grabbing that value. So that's the standard functionality for some kind of session store.
05:09
So now we'd expect to see the value Mo. So this is now in a session but then we're just pulling it back out. Now what happens if while I'm developing my project or if I'm taking on a new project, so the kind of base functionality already existed and I wanted to now store things in
05:26
a file. Well, what I don't want to do is go and update all of these, all of this code, like I don't even want to think about how this works behind the scenes. All I want to do is say, well, now I want to use a file store instead and I just want
05:44
to switch that out very quickly and I want this to work exactly as it worked before. So let's just import this at the top. We haven't actually built this class but let's go ahead and create a new file and let's call this file store.php and you may have guessed what we're going to do here, we're going to
06:02
implement that contract again. So let's just create our very basic namespace here and let's create our file store class and we're going to implement the storage interface. So again, if I run this, we know that we need them two methods that we have declared in
06:26
that contract. So over in our file store, we create them two methods. So we know it has to adhere to something. We know that this has to now have a set and a get method because it was previously used.
06:40
So here we just say set and we will say get and for set we obviously use a key and a value and for get, we just need a key. So we'll just do this very basically again, we'll just say file put contents and we'll store this under key and then we'll just append .txt on.
07:05
Obviously you would make this a little bit better, check if the file exists and things and then to get a file content, we just say file get contents and then we give the key and that's .txt. So now because we've created this contract, we've created a new class and remember we
07:25
can switch these out anytime. It doesn't matter because they both do the same thing because we've adhered to this contract. Now when I go and refresh here, it works in exactly the same way. We're now using the same functionality, we're just putting it in a different place.
07:41
So therefore our two concrete classes which are these two here which implement this interface, it doesn't really matter what they do, they do exactly the same thing. All we're doing is making sure that it adheres to the same functionality and that is the very basis of why we use an interface.
1 episode 8 mins

Overview

Interfaces are used in development a lot, but why make a class adhere to an defined interface? Let's take a look at an example of how this is useful.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!