Associative Arrays7:36 with Alena Holligan
Now that you understand how Getters and Setters work, let’s take a closer look at how we can use them when working with an associative array. This can give us detailed of control over what data we accept and what we do with that data.
We created our first getter and setter methods on the title property 0:00 because we want to control the incoming format. 0:04 The next property in the class is an array of ingredients. 0:07 We could set this up as a simple array of strings, these strings 0:11 would be the full ingredients just as you would see them in a recipe. 0:15 Two cups flour, one egg, a cup and a half of milk, and so on. 0:19 This would work fine, if we just want to print out a list of ingredients. 0:24 It does make it harder to control the format this way. 0:29 Are we going to accept numbers such as fractions, one-half, 0:32 or decimals, like 0.5? 0:36 It also makes it more difficult when it comes to combining ingredients 0:40 to make a shopping list, which is something we'll be doing a little later. 0:44 Instead of using a single string for 0:48 each ingredient, we're going to set up our ingredients as an associative array. 0:50 This will allow us to break our ingredients up into parts, and 0:55 better control the formatting and merging of items. 1:00 Let's look at an example of how the data will be stored. 1:03 Each ingredient will be an array with three keyed elements. 1:07 Items such as flour. 1:11 Amount, such as 2.5. 1:13 And measurement, such as cup. 1:15 We'll add more ingredients and 1:18 each of these individual ingredients will then be stored in a basic numbered array. 1:20 Let's start adding the ingredients. 1:25 Instead of just setting the property all at once, 1:28 we're going to add individual ingredients. 1:30 So we'll use public function addIngredient. 1:33 This method will accept three arguments, 1:40 item, amount and measure. 1:45 Only the first item will be required. 1:48 So we can add a default value of null to both the amount and the measure. 1:51 This will allow us to accept things like, egg, one, without a measurement, 2:03 or maybe lime to taste, without an amount or a measurement. 2:09 After accepting these three arguments, 2:13 we want to add them to our ingredients as an associative array. 2:15 This ingredient, we'll add an element and 2:19 our element will be an array. 2:24 We'll have a key of item. 2:30 And we'll set this to item. 2:31 Our next key will be amount and we'll set this to amount. 2:37 And finally measure. 2:44 We can also format these values like using, UC words, on our item. 2:52 We can also require that they pass a float or an integer as the amount. 3:02 Before setting this in gradient, 3:06 we'll check if amount is not equal null and 3:12 or not is_float amount and 3:20 not is_int amount. 3:24 If we haven't passed a float or an integer, 3:30 then we want to exit our script with a message. 3:32 We can also add more information about what they did pass. 3:43 Get type, tells us which type the amount is. 3:49 We can also require that they pass a valid measurement. 4:02 To do this we have to first define which measurements we're going to accept. 4:06 To do this, we're going to add another private property. 4:11 Private because we don't want anything to be able to change our measurements. 4:20 We'll call these measurements and we'll set it to an array. 4:25 We want tsp, tbsp, 4:30 cup, oz, pound, 4:36 fluid ounce, pint, 4:42 quart and gallon. 4:48 You can change these however you want or add other values. 4:57 Now that we have defined the acceptable measurements we can make sure that 5:02 the measurement that we passed in is valid. 5:05 If measure is not equal 5:13 to null and In array, 5:19 check measure in this 5:24 measurements. 5:29 Again, we want to exit our script with a message. 5:33 We can tell them which measurements are valid by using implode. 5:45 I also want to make sure that it's checking the lowercase version of measure. 5:59 STR for straying too lower. 6:04 I also want to set the lowercase version of measure. 6:11 Let's see if we can get some errors. 6:18 Let's addIngredient. 6:26 Press egg and the word one and then dozen. 6:31 Now let's run this script in the console. 6:38 The amount must be a float: string one given. 6:47 Okay, great. 6:52 Let's change this string to an int. 6:53 Now let's try running it again. 6:57 Undefined property. 6:58 Let's look up here. 7:05 This should be an s for measurements. 7:06 Now let's run it again. 7:12 Great, now we see please enter a valid measurement with our list of measurements. 7:15 We can now take off the measurement and try it again. 7:20 Great, no errors. 7:29 But we don't have any way to retrieve the ingredients yet. 7:30 We need to add a new method. 7:34
You need to sign up for Treehouse in order to download course files.Sign up