Adding Properties2:56 with Alena Holligan
We call the variables inside a class, properties. Properties can accept values like strings, integers, and booleans (true/false values), like any other variable. We demonstrate how to use properties by adding an array for the recipe ingredients.
Variables within a class are called properties.
The naming convention for properties is camelCase, like a camel, the “humps” are in the middle. The name starts with a lowercase letter, and all other words, start with a capital letter. For example, $ingredients or $dryIngredients.
Access modifiers allows us to control the access, or visibility, of our properties. These access modifiers are public, private, or protected. If declared using var, the property will be defined as public. When declaring a property, the visibility MUST be defined by an access modifier.
Public: Publicly accessible from anywhere, even from outside the scope of the class.
Private: Accessed within the class itself. It protects properties and methods from being accessed from outside the class.
Protected: Same as private, except by allowing child (sub) classes to access protected parent (super) properties and methods.
To add data to a class we use properties or class specific variables. 0:00 These work exactly like any other variable except that they belong to the object, 0:05 and therefore can only be accessed using the object. 0:10 When declaring a property, the visibility must be defined by an access modifier. 0:14 These access modifiers are public, private, or protected. 0:20 The access modifiers allow us to control access to our properties. 0:24 We'll demonstrate how visibility affects our properties a little later. 0:30 For now, we'll use public. 0:34 Let's add the properties for our recipe class. 0:36 We put the public keyword in front of a class property. 0:40 The naming convention for properties is camelCase. 0:43 Like a camel the humps are in the middle. 0:48 The name starts with the lower case letter and 0:51 all other words start with a capital letter. 0:53 For example, $ingredients. 0:56 Or $dryIngredients. 1:00 For this project, we're just going to use a single ingredients property. 1:07 We can create a property with or without a default value. 1:13 For example, we can create a property for source so 1:17 that we know where this recipe came from. 1:22 By default these are going to be my own recipes, so 1:25 I can set the default source to Alena Holligan. 1:28 In contrast, we want each recipe to have its own title. 1:34 So we can create a title property. 1:39 Our title property would not have a default value. 1:44 We can also initialize a property like any other variable 1:49 by initializing ingredients. 1:53 With an empty array, we make our class easier to read and 1:55 we know right up front that our ingredients will be stored in an array. 2:01 We have a few more properties to add. 2:06 We want to add instructions. 2:11 We'll make those an array as well, then we'll 2:16 add Yield without a default value. 2:21 Finally, most cookbooks are organized in some way, 2:27 usually by category but we could really group these any way we want. 2:31 Let's add a generic property called tag. 2:36 We'll initialize this as an array, as well. 2:43 Now that we have our properties set up. 2:47 We're ready to start accessing those properties and 2:48 creating independent objects. 2:51 That can each have different values, for those properties. 2:53
You need to sign up for Treehouse in order to download course files.Sign up