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 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
}
?>