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

Pontus Bolmér
Pontus Bolmér
12,471 Points

Cookbook problem

Im having some issues with this cookbook, not sure what's going wrong..It says this

displayRecipePHP Notice: Undefined variable: belgian_waffles in /home/treehouse/workspace/classe
s/cookbook.php on line 54
PHP Fatal error: Uncaught Error: Call to a member function getTitle() on null in /home/treehouse
/workspace/classes/render.php:26

I have looked at it 100 times, i dont get whats wrong here...

Here is my code from recipes.php

<?php class Recipe {

private $title;
private $ingredients = array();
private $instructions = array();
private $yield;
private $tag = array();
private $source = "Pontus Bolmér";

private $measurements = array (
  "tsp",
  "tspb",
  "cup",
  "oz",
  "lb",
  "fl oz",
  "pint",
  "quart",
  "gallon"
);


public function addIngredient ($item, $amount = null, $measure = null) {
    if($amount != null && !is_float($amount) && !is_int($amount)) {
      exit("The amount must be a float: " . gettype($amount) . " $amount give");
    }
  if($measure != null && !in_array(strtolower($measure), $this->measurements)) {
    exit("Please enter a valid measurement: " . implode(", ", $this->measurements));

  }
    $this->ingredients[] = array (
      "item" => ucwords($item),
      "amount" => $amount,
      "measure" => strtolower($measure)
    );

}

public function getIngredients() {
  return $this->ingredients;
}

public function addInstruction($string) {
  $this->instructions[] = $string;
}

public function getInstructions() {
  return $this->instructions;
}

public function addTag($tag) {
  $this->tags[] = strtolower($tag);
}


public function getTags() {
  return $this->tags;
}

public function setYield($yield) {
  $this->yield[] = $yield;
}

public function getYield() {
  return $this->yield;
}

public function setSource($source) {
  $this->source = ucwords($source);
}

public function getSource () {
  return $this->source;
}

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


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


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


public function __toString() {
$output = "You are calling a " . __CLASS__ . " object with the title \"";
 $output .= $this->getTitle() . "\"";
 $output .= "\nIt is stored in " . basename(__FILE__) . " at " . __DIR__ .".";
 $output .= "\nThis display is from the line " .__LINE__ . " in method " . __METHOD__;
 $output .= "\nTHe following methods are available for  " . __CLASS__ . " objects: \n"; 
 $output .= implode("\n" ,get_class_methods(__CLASS__));
 return $output;
}

} ?>

and cookbook.php <?php include "classes/recipes.php"; include "classes/render.php"; include "classes/recipeCollection.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); var_dump($cookbook);

$recipe1 = new Recipe("My first recipe"); $recipe1->setSource = "Grandpa Pontus"; $recipe1->addIngredient("egg",1); $recipe1->addIngredient("flour",2, "cup");

$recipe2 = new Recipe("My second recipe"); $recipe2->setSource = "Grandpa Percy ";

$recipe1->addInstruction("This is my first instruction"); $recipe1->addInstruction("This is my second instruction");

$recipe1->addTag("Breakfast"); $recipe1->addTag("Main course");

$recipe1->setYield = ("6 servings");

echo $recipe1;

$test = new Render();

echo Render::displayRecipe($belgian_waffles);

?>

and render.php <?php class Render {

public function toString() { $output .= " "; $output .= "\nTHe following methods are available for " . __CLASS . " objects: \n"; $output .= implode("\n" ,get_class_methods(CLASS)); return $output; }

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

}

public static function displayRecipe($recipe) { //return $recipe->getTitle() . " by " . $recipe->getSource(); $output = " ";

 $output .= $recipe->getTitle() . " by " . $recipe->getSource();
 $output .= "\n\n";
 $output .= implode(", ", $recipe->getTags());
 $output .= self::listIngredients($recipe->getIngredients());
 $output .= "\n";


  $output .= "\n";

 $output .= implode("\n", $recipe->getInstructions());
 $output .= "\n";
 $output .= $recipe->getYield();
 return $output; 

}

}

?>

and 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->recipe[] = $recipe; }

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

}

?>

1 Answer

Andrey Gamanek
Andrey Gamanek
3,912 Points

Hi i think the problem is in accessing the properties ... did you try to change the class properties as public to check if it works?