1 00:00:00,025 --> 00:00:05,000 [MUSIC] 2 00:00:05,000 --> 00:00:08,580 Congratulations on creating and using your first class. 3 00:00:08,580 --> 00:00:12,580 Now that we have all these recipes what do we do with them? 4 00:00:12,580 --> 00:00:16,110 Organization helps us use things more effectively. 5 00:00:16,110 --> 00:00:19,710 And the first step for organization is grouping things. 6 00:00:19,710 --> 00:00:22,130 So how would we group recipes? 7 00:00:22,130 --> 00:00:24,160 In a cookbook of course. 8 00:00:24,160 --> 00:00:26,330 I'm a bit of a cookbookaholic. 9 00:00:26,330 --> 00:00:29,060 I drastically cut my collection when we moved. 10 00:00:29,060 --> 00:00:31,639 And I try to avoid that section of the bookstore now. 11 00:00:31,639 --> 00:00:34,255 But what does that mean for our program? 12 00:00:34,255 --> 00:00:38,500 We could simply group all our objects in an array. 13 00:00:38,500 --> 00:00:41,120 We've done this type of thing before and 14 00:00:41,120 --> 00:00:44,720 an array, can be extremely powerful in its own right. 15 00:00:45,800 --> 00:00:50,240 Especially when used with PHP's built in array functions. 16 00:00:50,240 --> 00:00:54,290 But we want our cookbook, to be more than just a group of objects. 17 00:00:54,290 --> 00:00:58,310 We want this group to be able to perform some actions 18 00:00:58,310 --> 00:01:02,250 such as filter by property or give us a shopping list. 19 00:01:03,270 --> 00:01:06,100 Can we create an object of objects? 20 00:01:06,100 --> 00:01:07,630 Yes we can. 21 00:01:07,630 --> 00:01:13,750 A collection object can hold a group of objects, typically stored in array. 22 00:01:13,750 --> 00:01:19,720 A collection object also allows us to perform actions on the stored objects. 23 00:01:19,720 --> 00:01:23,480 Most data persistence frameworks have their own implementation of 24 00:01:23,480 --> 00:01:25,380 the collection object. 25 00:01:25,380 --> 00:01:30,100 And, there are some more advanced options that I'll link to in the teachers notes. 26 00:01:30,100 --> 00:01:32,680 For this project, we're going to create 27 00:01:32,680 --> 00:01:36,650 a simple collection class with the specific actions that we want to use. 28 00:01:36,650 --> 00:01:41,330 To start with, we're going to create a new class. 29 00:01:41,330 --> 00:01:44,410 So we need to create a new file in our classes folder. 30 00:01:46,340 --> 00:01:51,657 We'll name this recipecollection.php. 31 00:01:53,372 --> 00:01:59,820 We open our php tag, and then we create a new class called RecipeCollection. 32 00:02:03,760 --> 00:02:06,440 Again, we're using studly caps. 33 00:02:06,440 --> 00:02:09,170 We're going to use two properties, a title. 34 00:02:11,741 --> 00:02:17,190 To name our collection and a recipes array to store our recipes. 35 00:02:19,890 --> 00:02:23,763 Like we did with the recipe class, we'll make these properties private and 36 00:02:23,763 --> 00:02:26,120 we'll add getters and setters. 37 00:02:26,120 --> 00:02:29,800 We can use the getter and setter for the title directly from our recipe class. 38 00:02:34,634 --> 00:02:36,950 We can also grab the constructor as well. 39 00:02:45,405 --> 00:02:48,581 This will allow us to give it a title at the same time as we instantiate 40 00:02:48,581 --> 00:02:49,970 our object. 41 00:02:49,970 --> 00:02:52,710 Next we need to be able to add a recipe. 42 00:02:52,710 --> 00:02:53,720 Let's create a new method. 43 00:02:57,198 --> 00:02:59,270 We'll call this add recipe. 44 00:02:59,270 --> 00:03:06,015 We're going to accept a recipe And add it to our array 45 00:03:14,460 --> 00:03:19,789 Finally, we need to add public function getRecipes. 46 00:03:22,832 --> 00:03:27,520 And then we return this recipes. 47 00:03:30,510 --> 00:03:33,360 Let's go back into our cookbook and create a cookbook object. 48 00:03:35,180 --> 00:03:37,370 First we need to include our new class file. 49 00:03:48,160 --> 00:03:51,110 Next, we can instantiate a cookbook object. 50 00:03:55,870 --> 00:03:59,923 We'll use new RecipeCollection and 51 00:03:59,923 --> 00:04:05,420 then we'll pass in a title, Treehouse Recipes. 52 00:04:08,630 --> 00:04:12,770 Now we can add the recipes to our cookbook collection using the add recipe method. 53 00:04:19,190 --> 00:04:25,510 If we go into our recipes file, we can grab all of our recipes. 54 00:05:01,780 --> 00:05:04,880 Now let's try a var_dump on our cookbook object. 55 00:05:12,612 --> 00:05:13,490 Let's hide this line. 56 00:05:23,794 --> 00:05:24,820 Now let's run our script. 57 00:05:33,336 --> 00:05:34,548 Class. 58 00:05:34,548 --> 00:05:37,198 Not new. 59 00:05:44,242 --> 00:05:45,730 Wow that's a lot of information. 60 00:05:45,730 --> 00:05:49,630 But that means we have a basic group for the collection object. 61 00:05:49,630 --> 00:05:53,310 But it's not really doing any more than just getting and setting an array. 62 00:05:53,310 --> 00:05:55,200 So let's start enhancing our collection.