Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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.
These work exactly like any other variable except that they belong to the object,
and therefore can only be accessed using the object.
When declaring a property, the visibility must be defined by an access modifier.
These access modifiers are public, private, or protected.
The access modifiers allow us to control access to our properties.
We'll demonstrate how visibility affects our properties a little later.
For now, we'll use public.
Let's add the properties for our recipe class.
We put the public keyword in front of a class property.
The naming convention for properties is camelCase.
Like a camel the humps are in the middle.
The name starts with the lower case letter and
all other words start with a capital letter.
For example, $ingredients.
For this project, we're just going to use a single ingredients property.
We can create a property with or without a default value.
For example, we can create a property for source so
that we know where this recipe came from.
By default these are going to be my own recipes, so
I can set the default source to Alena Holligan.
In contrast, we want each recipe to have its own title.
So we can create a title property.
Our title property would not have a default value.
We can also initialize a property like any other variable
by initializing ingredients.
With an empty array, we make our class easier to read and
we know right up front that our ingredients will be stored in an array.
We have a few more properties to add.
We want to add instructions.
We'll make those an array as well, then we'll
add Yield without a default value.
Finally, most cookbooks are organized in some way,
usually by category but we could really group these any way we want.
Let's add a generic property called tag.
We'll initialize this as an array, as well.
Now that we have our properties set up.
We're ready to start accessing those properties and
creating independent objects.
That can each have different values, for those properties.
You need to sign up for Treehouse in order to download course files.Sign up