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
By defining another interface method, we'll explore another feature of abstract classes. When a class is defined as abstract, it does not need to define all methods. Instead it passes that requirement to the next NON-abstract class.
-
0:01
Currently, the collection class is abstract and
-
0:05
it implements the CollectionInterface.
-
0:07
All methods from the interface are being implemented in the collection class.
-
0:12
Let's add one more method to that interface.
-
0:15
We'll call this public function getTitle.
-
0:22
Now let's preview our site again.
-
0:25
We get another error like we've seen before because we have not
-
0:28
implemented our required method.
-
0:30
But let's take a closer look at that error.
-
0:32
First, notice that the error is on the post class, not on the collection class.
-
0:39
The next part of the error explains why.
-
0:42
The abstract method must either be implemented or
-
0:46
the class itself may be declared abstract.
-
0:50
Because the collection class is abstract.
-
0:52
And therefore cannot be instantiated,
-
0:55
it is not required to implement all the required methods itself.
-
1:00
Instead, this requirement is passed to any non-abstract class,
-
1:05
extending the collection class.
-
1:07
Let's add this method to the post class.
-
1:20
Public function getTitle.
-
1:27
First, if this count is equal to 1,
-
1:37
This means that we're on a single post page.
-
1:41
If we're on a single post page, we want to return the current title.
-
1:50
If we make it past this return, we'll simply return Latest Posts.
-
1:59
Let's clean up our pages.
-
2:00
In header.php, in our views folder, We used the title twice.
-
2:08
Let's replace those with this new method instead, content getTitle.
-
2:27
Now we can remove the title from the index page.
-
2:43
Since we don't need to set the title, we can make this a single if statement.
-
2:49
So, if our content is not set or
-
2:55
the content count is not equal to 1, or,
-
3:06
The content current status does not equal published.
-
3:15
Then we will select all posts.
-
3:17
Once again,
-
3:18
let's preview the site to make sure everything is still functioning properly.
-
3:27
Looks good.
-
3:28
We're ready to add a second content type for
-
3:31
pages by extending a second child of the collection class.
You need to sign up for Treehouse in order to download course files.
Sign up