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 the Recipe Adding Getters and Setters

Errors, Undefined index??

https://teamtreehouse.com/library/adding-getters-and-setters

When I run the code I get some code back and errors: My First RecipePHP Notice: Undefined index: measeure in /home/treehouse/workspace/cookbook.php on line 16

Notice: Undefined index: measeure in /home/treehouse/workspace/cookbook.php on line 16

1 EggPHP Notice: Undefined index: measeure in /home/treehouse/workspace/cookbook.php on line 16

Notice: Undefined index: measeure in /home/treehouse/workspace/cookbook.php on line 16
thrown in /home/treehouse/workspace/cookbook.php on line 22

Fatal error: Uncaught Error: Call to undefined method Recipe::getInstuction() in /home/treehouse/workspace/cookbook.php:22
Stack trace:

0 {main}

thrown in /home/treehouse/workspace/cookbook.php on line 22

cookbook.php

php

<?php
include "classes/recipe.php";

$recipe1 = new Recipe();
//$recipe1->source = "Nana Dale";
$recipe1->setTitle("my first recipe");
$recipe1->addIngredient("egg", 1, "oz");
$recipe1->addIngredient("flour", 2, "cups");

$recipe2 = new Recipe();
//$recipe2->source = "Minimalist Baker";
$recipe2->setTitle("my second recipe");

echo $recipe1->getTitle();
foreach ($recipe1->getIngredients() as $ing) {
  echo "\n" . $ing["amount"] . " " . $ing["measeure"] . " " . $ing["item"];
}

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

echo implode("\n", $recipe1->getInstuction());

recipe.php

php

<?php

class Recipe 
{
  private $title;
  private $indredients = array();
  private $instructions = array();
  private $yeild;
  private $tag = array();
  private $source = "Anna Poff";

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

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

  public function addIngredient($item, $amount = null, $measure = null)  {
    if ($amount != null && !is_float($amount) && !is_int($amount)) {
      exit("The amount must ba a float: " . gettype($amount) . " $amount given"); 
    }
        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 displayRecipe() {
    return $this->title . " by " . $this->source;
  }
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I feel fairly certain this is due to a typo. Check out this line:

echo "\n" . $ing["amount"] . " " . $ing["measeure"] . " " . $ing["item"];

It's looking for something at the key of "measeure", but that should be "measure".

echo "\n" . $ing["amount"] . " " . $ing["measure"] . " " . $ing["item"];

Note the removal of the extra "e" from "measeure" to "measure".

Hope this helps! :sparkles:

This DID help some! Sorry, nothing worse than a dyslexic trying to code! After correcting this, I'm still getting

PHP Fatal error: Uncaught Error: Call to undefined method Recipe::getInstruction() in /home/treehouse/workspace/cookbook.php:22
Stack trace:

0 {main}

thrown in /home/treehouse/workspace/cookbook.php on line 22

Fatal error: Uncaught Error: Call to undefined method Recipe::getInstruction() in /home/treehouse/workspace/cookbook.php:22
Stack trace:

0 {main}

thrown in /home/treehouse/workspace/cookbook.php on line 22

Any other suggestions? I've been hung up here for hours!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

On this line you're calling the getInstruction() function.

echo implode("\n", $recipe1->getInstuction());

It's the last line in your cookbook.php. Unfortunately, you don't have a getInstruction function currently. You do however have a getInstructions function. Notice the difference between singular and plural. You have this as the next to last function in your recipe.php file.

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

I believe that the first line I mentioned should be altered to:

echo implode("\n", $recipe1->getInstuctions());

Hope this helps! :sparkles: