Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Object-Oriented PHP Basics Building a Collection Creating a Meal Plan

Hi all, anyone able to help me solve an error I get with echo Render::listRecipes($cookbook->getRecipeTitles());

Hi all I am getting the following error:

treehouse:~/workspace$ php cookbook.php
PHP Warning: asort() expects parameter 1 to be array, null given in /home/treehouse/workspace/classes/render.php o n line 40
PHP Warning: Invalid argument supplied for foreach() in /home/treehouse/workspace/classes/render.php on line 42
treehouse:~/workspace$

When calling:

echo Render::listRecipes($cookbook->getRecipeTitles());

from the cookbook file.

I have included all the other relevant files into cookbook:

include "classes/recipes.php"; include "classes/render.php"; include "classes/recipecollection.php"; include "inc/allrecipes.php";

....And these are the relevant methods:

public static function listIngredients($ingredients) { $output = ""; foreach ($ingredients as $ing) { $output .= $ing["amount"] . " " . $ing["measure"] . " " . $ing["item"]; $output .= "\n"; } return $output; }

public function getRecipeTitles() { $titles = array(); foreach ($this->recipes as $recipe) { $titles[] = $recipe->getTitle(); } }

public function getTitle() { return $this->title; }

Their parent classes have been working ok so far so I assume the problem is not to do with that.

Any help welcome as always : )

1 Answer

getRecipeTitles() is missing a return statement.

<?php
public function getRecipeTitles() 
{ 
    $titles = array(); 
    foreach ($this->recipes as $recipe) { 
        $titles[] = $recipe->getTitle(); 
    } 
    return $titles; // Without this line, nothing will be returned from the function
}
?>