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