Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Implementing Interface Methods4:55 with Alena Holligan
Every abstract method from any interface an object implements MUST be defined. We'll follow the documentation for Iterator and Countable to implement the required methods in the Collection class.
Let's take a look at that documentation. 0:00 The iterator interface requires five methods. 0:16 Current, key, next, rewind, and valid. 0:20 The countable interface requires a single count method. 0:30 Just like with our own interfaces, PHP will also throw exceptions 0:34 if we try to implement a built in interface without the required methods. 0:39 Since we are instantiating our collection class, 0:44 let's try previewing our site again. 0:46 We see the error telling us which methods need to be implemented. 0:51 So let's get to work. 0:55 For each public method we'll be creating, 0:57 we'll use a built-in function on our collection array. 0:59 For the iterator method, each function will be working with the array pointer. 1:02 The pointer saves the position on an array. 1:07 For public function current(), 1:09 we're going to return current 1:15 ($this->collection);. 1:21 This will return the current pointer of the collection array. 1:27 We can duplicate this method for other five methods. 1:31 For key, the function is called key(). 1:41 This gives you the key of the item the pointer is on. 1:49 For the method next, The function call is next. 1:54 For the method rewind((), The function call is actually reset. 2:07 This resets the pointer of the array to the first item. 2:18 For the method valid(), We're going to check the key 2:24 And see if it's not equal to null. 2:32 This will let us know if the current key is valid. 2:36 For count(), we'll us our common count function, 2:41 Which returns the number of items in the array. 2:48 Before we give our new collection a try, 2:50 lets add a vardump that will help us see what's going on. 2:52 In each of the iterator methods, we're going to add var_dump(_ _ and 2:56 then the magic constant METHOD. 3:02 Rewind, next key and 3:10 current. 3:16 We're ready to give our new collection a try. 3:19 So let's open index.php. 3:20 For our collection, we need to prse in the $repo. 3:22 By default, this will pull all the posts. 3:29 Let's loop through each post and return the title. 3:32 Foreach ($content as $item). 3:41 Echo $item-> title. 3:48 And let's check this out in the browser. 3:52 Let's view the source. 3:56 Let's increase this font size. 4:01 For the first pass, we can see that it rewinds the pointer. 4:07 Then it checks that the current item is valid. 4:12 If so, it returns the current item. 4:15 And this is where we get our title. 4:18 The following loops move the pointer to the next item 4:21 before checking that the item is valid and then returning current. 4:25 After the last item, the call to valid will return false and the loop will end. 4:31 Great, now that we've seen what happens, let's go back and 4:36 comment out those method dumps. 4:39
You need to sign up for Treehouse in order to download course files.Sign up