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 Enhancing the Collection

Roger Dailey
Roger Dailey
14,887 Points

Completely Lost!!!

I have done everything exactly like she did in the video and I keep getting a fatal error when I try and run cookbook.php in the console:

treehouse:~/workspace$ php cookbook.php                                         PHP Fatal error:  Uncaught Error: Call to a member function getTitle() on str   ing in /home/treehouse/workspace/classes/recipecollection.php:41                Stack trace:                                                                   

0 /home/treehouse/workspace/cookbook.php(26): RecipeCollection->getRecipeTit  

les()                                                                          

1 {main}                                                                      

  thrown in /home/treehouse/workspace/classes/recipecollection.php on line 41                                                                                   Fatal error: Uncaught Error: Call to a member function getTitle() on string i   n /home/treehouse/workspace/classes/recipecollection.php:41                     Stack trace:                                                                   

0 /home/treehouse/workspace/cookbook.php(26): RecipeCollection->getRecipeTit  

les()                                                                          

1 {main}                                                                      

  thrown in /home/treehouse/workspace/classes/recipecollection.php on line 41

cookbook.php

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

$cookbook = new RecipeCollection("Treehouse Recipes");
$cookbook->addRecipe("$lemon_chicken");
$cookbook->addRecipe("$granola_muffins");
$cookbook->addRecipe("$belgian_waffles");
$cookbook->addRecipe("$pepper_casserole");
$cookbook->addRecipe("$lasagna");
$cookbook->addRecipe("$dried_mushroom_ragout");
$cookbook->addRecipe("$rabbit_catalan");
$cookbook->addRecipe("$grilled_salmon_with_fennel");
$cookbook->addRecipe("$pistachio_duck");
$cookbook->addRecipe("$chili_pork");
$cookbook->addRecipe("$crab_cakes");
$cookbook->addRecipe("$beef_medallions");
$cookbook->addRecipe("$silver_dollar_cakes");
$cookbook->addRecipe("$french_toast");
$cookbook->addRecipe("$corn_beef_hash");
$cookbook->addRecipe("$granola");
$cookbook->addRecipe("$spicy_omelette");
$cookbook->addRecipe("$scones");
echo Render::listRecipes($cookbook->getRecipeTitles());

recipecollection.php

<?php

class RecipeCollection
{
  private $title;
  private $recipes = array();

  public function __construct($title = NULL) 
  {
      $this->setTitle($title);
  }

  public function setTitle($title)
  {
      if (empty($title)) {
          $this->title = NULL;
      } else {
          $this->title = ucwords($title);
      }
  }

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

  public function addRecipe($recipe)
  {
      $this->recipes[] = $recipe;
  }

  public function getRecipes()
  {
      return $this->recipes;
  }

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

1 Answer

Judging by the code you have posted, you are passing in a string literal as the argument for each addRecipe function call where it should be a reference to the variable object i.e. addRecipe("$lemon_chicken") -> should be addRecipe($lemon_chicken), otherwise the argument that is being passed in is literally a string that says "$lemon_chicken" rather than the value stored in the $lemon_chicken object

Roger Dailey
Roger Dailey
14,887 Points

WOW!!! I cannot believe I over looked that. I went back and re-wrote all the methods and still didn't work. Now that you pointed it out I feel like an idiot. Thank you so much.