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 trialilanaguttman
2,700 PointsNothing happens when I run cookbook.php
Not sure what happened. I'm up to Magic Methods and now nothing happens when I run cookbook.php in the console.
My recipes.php code: ''' <?php
class Recipe { public $title; public $ingredients = array(); public $instructions = array(); public $yield; public $tag = array(); public $source = "Alena Holligan";
private $measurement = array( "tsp", "tbsp", "cup", "oz", "lb" );
/* public function __construct($title = null) { if (empty($title)){ $this->$title = null; } else{ $this->title = ucwords($title); } }*/
public function __toString(){ return $this->getTitle(); }
public function setTitle($title){ $this->title = ucwords($title); }
public function getTitle(){ return $this->title; }
public function getSource(){ return $this->source; }
public function getInstructions(){ return $this->instructions; }
public function getYield(){ return $this->yield; }
public function addIngredients($item, $amount = null, $measure = null){ if ($amount != null && !is_float($amount) && !is_int($amount)){ exit("The amount must be a float: " . gettype($amount) . " $amountgiven"); } if ($measure !=null & !in_array(strtolower($measure), $this->measurement)) { exit("Please enter a valid measurement: " . implode(", ", $this->measurement)); } $this->ingredients[] = array( "item" => ucwords($item), "amount" => $amount, "measure" => strtolower($measure) ); }
public function getIngredients(){ $this->ingredients; }
}
'''
My cookbook.php code: ''' <?php
include "classes/recipes.php"; include "classes/render.php";
$recipe1 = new Recipe(); $recipe1->source = "Grandma Holligan"; $recipe1->addIngredients("egg", 1); $recipe1->addIngredients("flour", 2, "Cup");
$recipe2 = new Recipe(); $recipe2->source = "Betty Crocker";
echo Render::displayRecipe($recipe1);
'''
1 Answer
Jamiu Oloyede
345 PointsHi ilanaguttman,
You need to echo the recipe object for the __toString() magic method to work.
echo $recipe1;
```
David Evans
10,490 PointsDavid Evans
10,490 PointsIs there any error message in your console?
It seems you are missing several updates to your code ilanaguttman. You might want to go back through and re-watch some of the previous videos.
If you don't want to, below is my code so far with a slight change to cookbook.php due to the video having some incorrect code with the setSource method. I hope this helps.
recipes.php
cookbook.php
render.php