Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics Building a Collection Filtering

Oleksii Temchenko
Oleksii Temchenko
3,474 Points

Where does getTagArray() come from?

In recipecollection.php we have such func:

public function filterByTag($tag) { $taggedRecipes = array(); foreach ($this->recipes as $recipe) { if (in_array(strtolower($tag),$recipe->getTagArray())) { $taggedRecipes[] = $recipe; } } return $taggedRecipes; }

I wonder, what is $recipe->getTagArray()? I do not remembered creating such method in

ecipecollection.php, only getTags() in Recipes class. And even if it is created somewhere,

how does recipecollection.php "know" about it? Is it some kind of built-in language stuff?

2 Answers

Hey, at 01:40 you can see Alena use $recipe->getTags() not $recipe->getTagArray(). Where you see $recipe->getTagArray()?

Oleksii Temchenko
Oleksii Temchenko
3,474 Points

sorry, my fault. I must have taken this from downloaded files in next video. getTags() is method from recipes.php.

Am I right, that when we are doing $cookbook->addRecipe($lemon_chicken), $lemon_chicken comes with all methods of Recipe class, including getTags()? And thats why, when we are using this method in recipecollection.php we do not have to include recipes.php?

And why do not have to include recipes.php into allrecipes.php in order to create instance of Recipe class?

When we call $cookbook->addRecipe($lemon_chicken), we are passing an object instance of Recipe Class named lemon_chicken, that's why we can access all properties and methods from Recipe Class.

If you open cookbook.php, you can see we already include all files we needed. Remember, include basically copy the content of the files and combine it into 1 file. That's why RecipeCollection and allrecipes.php know about Recipe Class.

I have read through the answer but wonder where the method getTags() has been added or created for us to use it.