Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let’s clean up our object instantiation and output and move it out of our class file. Our cookbook will use more than one class so this is a great point to start separating the concerns.
Challenges
Now that we've seen how to get and set an associative array, try adding some new methods around ingredients. Such as:
- Converted to a simple array of ingredient strings
- Converted to just a string of ingredient items
- Return the ingredients formatted in a list
We'll call this getIngredients.
0:03
From this method I
return this ingredients.
0:10
I think we're ready to clean up our
object instantiation and output and
0:17
move it out of our class file.
0:20
So let's remove everything from this file.
0:23
Select all and cut, and
then we'll create a new file.
0:26
We'll call it cookbook.php.
0:32
We can paste this code into cookbook.php.
0:37
At the beginning of the file,
we need to open our php, and then we
0:41
need to include recipes.php.
0:46
But we also need to add
the folder classes.
0:50
Great, let's save these files.
0:59
Now in our console,
we run php cookbook.php.
1:03
And great, we return the same output.
1:11
Let's clean up this output and
then add some ingredients to recipe one.
1:14
Now let's loop through our ingredients,
after we get the title.
1:36
We'll set each of our ingredients to
an array called ing for ingredient.
1:48
Then we can echo out a new line for
each item.
1:58
Then we'll call the amount, like two
2:04
and then our measurement but
we also want to space in between so,
2:12
two cups.
2:17
And then we want the item,
again, another space.
2:24
Great, let's run our code.
2:35
We see My First Recipe,
1 Egg, 2 cup Flour.
2:38
Good job, that was quite a few
pieces to put together, but
2:43
you should have a pretty good idea of
the usefulness of getters and setters now.
2:46
We have the most complicated
property out of the way.
2:51
So we're ready to breeze through the rest
of these properties in the next video.
2:54
You need to sign up for Treehouse in order to download course files.
Sign up