‫So now we're going to look at something called interfaces, which are similar to abstract classes,
‫but they're not classes.
‫They're a way to define a contract for a class without having to define the implementation.
‫So an interface is a collection of abstract methods and constants.
‫And a class that implements an interface must implement all of the methods and constants defined in
‫that interface.
‫Okay.
‫And an interface cannot be instantiated.
‫So let's look at an example.
‫Let's say that we're building a content management system where we have various types of content like
‫articles, videos, images, and each type of content should have the ability to be displayed and edited.
‫So you want to ensure that all content types adhere to a common set of methods for displaying and editing
‫content, so you can use an interface to define that contract.
‫So I just have our basic template that we've been kind of just working with.
‫And I'm going to start off by just going up to the top here.
‫And let's open and close our PHP tag.
‫And to create an interface, we use the interface keyword.
‫We don't use class.
‫And let's call this content interface.
‫And what I'm going to do here is define public function display.
‫And we're not going to put anything in here just like we did with the the abstract.
‫Abstract function.
‫And I also want edit because remember we want a a common way for to display and edit content.
‫So this one will be called edit.
‫Okay, now let's implement this interface in a couple of different classes.
‫So the first one will be article.
‫And the way that we use it is just the same way that we use any other class that we're inheriting from.
‫I'm sorry that's wrong.
‫We're not using extends.
‫We're using implements.
‫I don't know why I said that.
‫So yeah, we use implements instead of extends.
‫And we want to extend the content interface.
‫All right.
‫And then we're going to have our private property of title.
‫And let's have our private property of content.
‫And then we'll have our public function constructor or construct.
‫And that's going to take in the title and the content.
‫And in here I'm just going to sign the title property to the title that's passed in.
‫Same thing with.
‫The content.
‫All right.
‫Now notice that I have this this red line here.
‫And it says article does not implement implement methods display and edit.
‫So since I implemented this interface I have to I have to use these functions right.
‫So let's come down here and say public function display.
‫And for this, what what I'm going to do is echo out a couple lines.
‫So we'll have an H.
‫Actually I'm going to use.
‫Double quotes here and let's have an H to.
‫And in that age too, we'll display the title.
‫So let's say.
‫This title.
‫All right.
‫And then I'm just going to copy that down and then I'll have a paragraph.
‫Paragraph that displays the.
‫Content.
‫All right.
‫And then for the edit let's say public function edit.
‫We're just going to go ahead and echo.
‫Editing.
‫The article.
‫And we'll put some single quotes around this and say this.
‫Content.
‫Or no.
‫I'm sorry.
‫This title.
‫So obviously in real life it would be some way to edit it.
‫But we're not.
‫Obviously we're not building a real system here, but so we now that have that red line gone because
‫we're implementing these these functions here.
‫All right.
‫So that's that's for the article.
‫Now let's say we have video content types as well.
‫So we'll say class video.
‫And we want to implement the content interface.
‫And again we have private title, but this time we're going to have private URL.
‫The video URL.
‫And then we want our constructor.
‫We can just grab this stuff here because we're going to have all three of these.
‫They'll just be a little a little bit different.
‫So for the constructor it's going to take in title and URL.
‫So I'm just going to replace these with URL.
‫And then for the display we'll go ahead and do the title and an H2.
‫But since it's in a video we're going to actually put it in an iframe.
‫So again this is a good example of polymorphism.
‫We have a display function.
‫But they're doing two different things in each of these classes.
‫So here I want it to be an iframe.
‫And we're going to set the source equal to.
‫Let's say this.
‫Earl.
‫And then.
‫Yeah, we'll close that and then I frame.
‫So we have a different we have different things going on for the displays.
‫And then for the edit we're going to change edit the video.
‫Editing the video and then the title.
‫So now we want to instantiate an article in a video.
‫So we'll say article set that to a new article.
‫And that takes in a title and the content.
‫So for the title say introduction to PHP.
‫And you can see we have this, this red line because we need to have both arguments the the title and
‫the content.
‫So the content we'll just say PHP is a versatile.
‫The scripting language and then for the video.
‫Let's set that to new video.
‫And we'll say PHP for.
‫Beginners and that takes in a URL, so I'm going to paste in a URL.
‫It's actually one of my YouTube videos.
‫And then I want to call the display.
‫Right.
‫So the display is going to have an H two and an I frame for the video and an H two and a paragraph for
‫the article.
‫So let's come down here to the template.
‫And I'm going to go right in here where it says output.
‫And get rid of that.
‫And let's we'll use the shorthand syntax here.
‫So equals.
‫And then let's do article.
‫Display.
‫And then I'm going to copy this.
‫We'll copy that down.
‫So they both have the card around them.
‫And then this one is going to be the video display.
‫So let's save that and reload.
‫And now you can see we get the article YouTube Views to connect.
‫Hmm.
‫I'm not sure why why it's doing that, but that's not really important.
‫The whole point of this video is just to show you how to implement an interface.
‫So any, any methods that you have here, you have to implement if you're going to be using that interface.
‫And you can use polymorphism here.
‫You can have different you can have the same methods do different things.
‫So as this one has an iframe, this one has a paragraph.
‫And you could create an image which has maybe an image source or something like that.
‫All right, so we've gone over just about all of the the fundamentals of object oriented programming.
‫So I think this is a good place to stop.
‫And in the next section we're going to start to look at something that's really important in PHP.
‫And that is super globals.