Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Cleaning Up the Class2:58 with Alena Holligan
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.
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.
From this method I return this ingredients.
I think we're ready to clean up our object instantiation and output and
move it out of our class file.
So let's remove everything from this file.
Select all and cut, and then we'll create a new file.
We'll call it cookbook.php.
We can paste this code into cookbook.php.
At the beginning of the file, we need to open our php, and then we
need to include recipes.php.
But we also need to add the folder classes.
Great, let's save these files.
Now in our console, we run php cookbook.php.
And great, we return the same output.
Let's clean up this output and then add some ingredients to recipe one.
Now let's loop through our ingredients, after we get the title.
We'll set each of our ingredients to an array called ing for ingredient.
Then we can echo out a new line for each item.
Then we'll call the amount, like two
and then our measurement but we also want to space in between so,
And then we want the item, again, another space.
Great, let's run our code.
We see My First Recipe, 1 Egg, 2 cup Flour.
Good job, that was quite a few pieces to put together, but
you should have a pretty good idea of the usefulness of getters and setters now.
We have the most complicated property out of the way.
So we're ready to breeze through the rest of these properties in the next video.
You need to sign up for Treehouse in order to download course files.Sign up