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