Creating a Collection5:55 with Alena Holligan
Organization helps us to use things more effectively. The first step in organization is grouping things. A collection object can hold a group of objects and allow us to perform actions on these objects. We're going to create a simple collection class with the specific methods we want to use.
[MUSIC] 0:00 Congratulations on creating and using your first class. 0:05 Now that we have all these recipes what do we do with them? 0:08 Organization helps us use things more effectively. 0:12 And the first step for organization is grouping things. 0:16 So how would we group recipes? 0:19 In a cookbook of course. 0:22 I'm a bit of a cookbookaholic. 0:24 I drastically cut my collection when we moved. 0:26 And I try to avoid that section of the bookstore now. 0:29 But what does that mean for our program? 0:31 We could simply group all our objects in an array. 0:34 We've done this type of thing before and 0:38 an array, can be extremely powerful in its own right. 0:41 Especially when used with PHP's built in array functions. 0:45 But we want our cookbook, to be more than just a group of objects. 0:50 We want this group to be able to perform some actions 0:54 such as filter by property or give us a shopping list. 0:58 Can we create an object of objects? 1:03 Yes we can. 1:06 A collection object can hold a group of objects, typically stored in array. 1:07 A collection object also allows us to perform actions on the stored objects. 1:13 Most data persistence frameworks have their own implementation of 1:19 the collection object. 1:23 And, there are some more advanced options that I'll link to in the teachers notes. 1:25 For this project, we're going to create 1:30 a simple collection class with the specific actions that we want to use. 1:32 To start with, we're going to create a new class. 1:36 So we need to create a new file in our classes folder. 1:41 We'll name this recipecollection.php. 1:46 We open our php tag, and then we create a new class called RecipeCollection. 1:53 Again, we're using studly caps. 2:03 We're going to use two properties, a title. 2:06 To name our collection and a recipes array to store our recipes. 2:11 Like we did with the recipe class, we'll make these properties private and 2:19 we'll add getters and setters. 2:23 We can use the getter and setter for the title directly from our recipe class. 2:26 We can also grab the constructor as well. 2:34 This will allow us to give it a title at the same time as we instantiate 2:45 our object. 2:48 Next we need to be able to add a recipe. 2:49 Let's create a new method. 2:52 We'll call this add recipe. 2:57 We're going to accept a recipe And add it to our array 2:59 Finally, we need to add public function getRecipes. 3:14 And then we return this recipes. 3:22 Let's go back into our cookbook and create a cookbook object. 3:30 First we need to include our new class file. 3:35 Next, we can instantiate a cookbook object. 3:48 We'll use new RecipeCollection and 3:55 then we'll pass in a title, Treehouse Recipes. 3:59 Now we can add the recipes to our cookbook collection using the add recipe method. 4:08 If we go into our recipes file, we can grab all of our recipes. 4:19 Now let's try a var_dump on our cookbook object. 5:01 Let's hide this line. 5:12 Now let's run our script. 5:23 Class. 5:33 Not new. 5:34 Wow that's a lot of information. 5:44 But that means we have a basic group for the collection object. 5:45 But it's not really doing any more than just getting and setting an array. 5:49 So let's start enhancing our collection. 5:53
You need to sign up for Treehouse in order to download course files.Sign up