Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Extending and combining interfaces, guarantees that all methods from all interfaces will be available in an implementing class.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I want to add another
requirement to the collection.
0:00
The ability to return a short description.
0:03
Instead of implementing another interface
I'm going to combine all of these
0:07
requirements into a single
collection interface.
0:11
This will allow us to be certain that
any class that implements a collection
0:14
interface will have all the iterator and
countable methods,
0:19
as well as the new method we'll define.
0:23
Add a new file to the source
interface's folder.
0:26
We'll name this CollectionInterface.php.
0:29
Interface CollectionInterface.
0:42
We'll define our new interface
by adding the keyword extends.
0:47
To extend the Iterator and
Countable interfaces.
0:54
Note that this is very similar to how
classes can extend other classes.
1:04
However, we can extend
more than one interface.
1:09
Now we can add our new method,
public function shortDescription.
1:12
By extending the iterator and countable
interfaces our CollectionInterface
1:22
will now include all of the methods
defined in those interfaces.
1:27
This means that anyone implementing our
CollectionInterface will also need to
1:32
implement the methods from the Iterator
and Countable interfaces as well.
1:37
Back in our Collection class.
1:41
Let's change this to CollectionInterface.
1:44
Now we need to define
the short description,
1:49
public function shortDescription.
1:55
We are going to use
the current item details.
2:04
First, we're going to check if
2:08
the strlen($this->current()->details)
2:12
Is less than 510, then we're
2:19
going to return the current details.
2:23
But we're going to strip the tags.
2:28
This current- Details.
2:33
If we make it past the early return,
2:42
we're going to return our
trimmed down version,
2:46
return, and
again we'll start with strip_tags.
2:51
I'm going to add some line spacing
to make this easier to read.
2:56
Substring this
3:00
current details.
3:04
We're going to start at our
first character, which is 0.
3:10
And then find the string
position In this current details.
3:14
And we're going to look for
the first space after 500 characters.
3:25
The final touch is to add an ellipses or
3:36
triple dots at the end to identify
that there's more to this description.
3:38
We'll just use dot, dot, dot.
3:43
We're finally ready to include our list
view with the post which will give us
3:47
a sample of each post.
3:51
Let's add a new view.
3:53
In the views folder, new file list.php.
3:55
The first thing I'm going to do is check
if the item status is equal to published.
4:04
Next we'll create an article
block with a title.
4:16
We'll add the $item,
title, and closer tag.
4:35
Finally we can use our short description.
4:42
Since the collection handles
the short description and
4:45
not the item itself, we use content.
4:49
So echo $content->shortDescription.
4:52
Since we've stripped tags,
5:00
let's add a paragraph tag
surrounding the short description.
5:01
Now we can add this to our index page.
5:15
In the for each loop,
instead of just the title
5:19
we're going to include 'views/list.php.
5:24
Let's take a look at this in the browser.
5:30
It's starting to look like a blog.
5:33
Let's add the ability to read each
of these posts in its entirety.
5:35
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up