Testing for Interfaces4:01 with Alena Holligan
One of the things that makes interfaces most useful is that you can apply them to many different classes. Those classes may not have anything else in common besides the one interface, but if we know that an object implements an interface we know that we can access any of the methods defined in that interface.
One of the things that makes interfaces most useful 0:00 is that you can apply them to many different classes. 0:03 Those classes may not have anything else in common besides the one interface and 0:07 that's fine. 0:12 The same interface can be applied to a collection class or a single object class. 0:13 The savory flavor interface that I explained in the first video 0:18 could be applied to a fruit or a vegetable. 0:22 It could even be applied to meat or grain. 0:25 The ability to check for specific interfaces allows us to use those methods 0:29 on any object that matches the interface. 0:34 This will allow our views to adapt to the content type 0:37 that they're currently displaying. 0:40 Let's check for our trackable and shareable interfaces and 0:43 display the details. 0:46 Let's start out in our list view. 0:48 After the title, let's add a conditional statement, 0:50 to check if content is an instanceof TrackableInterface. 0:56 Instance of works for classes, inherited classes, interfaces and 1:09 extended interfaces. 1:13 If it matches the interface, 1:16 we can include the track.php file from the current directory. 1:17 I've supplied the structure for 1:29 the track file but we're going to need to add the tracking methods. 1:30 Echo content getAuthor. 1:41 Echo content getDateTime. 1:50 Now we can close track.php. 1:55 This is all I want for the list view but 1:57 let's copy the conditional from the list view to use it on a single item view. 2:00 Let's duplicate this conditional for the ShareableInterface. 2:13 For the ShareableInterface, we'll include the share file instead. 2:22 Just like with the track file, I've supplied the structure. 2:27 We just need to go in and add the methods. 2:31 ShareTwitter, echo content, shareFacebook. 2:44 And finally, echo content, 2:56 shareEmail. 3:03 Now we can close this file. 3:06 One last thing before we preview these changes. 3:08 On this single page I want to wrap both these includes in a div. 3:11 echo div class=row. 3:14 Then we'll close our div. 3:27 Now let's preview these new changes on our site. 3:31 The list view page now displays the user, date, and time for each post. 3:35 When you click on an individual page post, along with the user, date, and 3:42 time, we also see the links for sharing on Twitter, Facebook, and by email. 3:47 Now, that's you've worked with interfaces, you're ready to explore abstract classes 3:54 in the similarities and differences they bring. 3:59
You need to sign up for Treehouse in order to download course files.Sign up