Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Object-Oriented PHP Basics!
You have completed Object-Oriented PHP Basics!
Preview
We have a basic structure for our collection object, but it's not really doing any more than getting and setting an array, so let's start enhancing our collection by pulling specific information from the objects.
Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We're using a collection object, instead
of an array, because we want to have more
0:00
control and keep the functionality of our
cookbook grouped together with the data.
0:04
So what are these extra features that
our cookbook brings to the party.
0:09
First off, we want to be able to
get a list of the recipe titles.
0:13
There are actually two parts to this
action, grabbing the titles and
0:18
displaying the list.
0:22
These are two very separate
responsibilities, grabbing the data, and
0:24
displaying the data.
0:28
In the last stage, we talked about
the Single Responsibility Principle, and
0:29
we set up a new render class.
0:34
The render class controls
how something is shown, and
0:36
can change based on where
we're using the data.
0:40
In this course, we're displaying
everything to the console, but
0:43
we could render our recipes
to a website or data feed.
0:47
No matter how or where we render this
data, the data remains the same.
0:51
Let's set up two methods.
0:57
One in our collection class
to grab the titles, and
0:59
one in the render class to
display these titles in a list.
1:03
Let's start in our recipe collection,
where we need to get the recipe titles.
1:08
Let's create a new method.
1:13
We'll name this getRecipeTitles().
1:18
Since each of the recipes is an object
that contains much more than just
1:27
the title, we need to grab just the title.
1:31
We'll start by initializing
our title's array.
1:35
Then we'll loop through each of
the recipes in the collection and
1:39
grab the title.
1:43
To our title's array,
we add $recipe getTitle():.
1:54
Then we can return our titles.
2:02
Now we can go into our render
class to display the list.
2:07
We'll call this listRecipes,
and we'll pass in the titles.
2:18
Before we display the recipes,
I think we should sort the titles.
2:28
We can use asort to sort
the values in the titles array.
2:33
Finally, we want to
return a list of titles.
2:40
So we'll use the implode function again.
2:42
Let's go back to our cookbook file and
try this out.
2:55
Instead of our var_dump,
let's use our Render.
3:01
Then we'll pass it our getRecipesTitles().
3:13
Now let's run this script.
3:21
Great, now we see a list of recipe
titles in alphabetical order.
3:26
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up