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 Magic Methods

Nothing 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);

'''

David Evans
David Evans
10,490 Points

Is 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

<?php

class Recipe
{
    private $title;
    private $ingredients = array();
    private $instructions = array();
    private $yield;
    private $tag = array();
    private $source = 'David Evans';

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

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

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

    public function setTitle($title) 
    {
        if (empty($title)) {
            $this->title = null;
        } else {
            $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 be 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 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;
    }
}

cookbook.php

<?php

include 'classes/recipes.php';
include 'classes/render.php';

$recipe1 = new Recipe('My First Recipe');
$recipe1->setSource('Grandma Evans');
$recipe1->addIngredient('egg', 1);
$recipe1->addIngredient('flour', 2, 'cup');

$recipe2 = new Recipe('my second recipe');
$recipe2->setSource('Betty Crocker');

$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;
echo Render::displayRecipe($recipe1);

render.php

<?php

class Render
{
    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)
    {
        $output = '';
        $output .= $recipe->getTitle() . ' by ' . $recipe->getSource();
        $output .= "\n";
        $output .= implode(', ', $recipe->getTags());
        $output .= "\n\n";
        $output .= self::listIngredients($recipe->getIngredients());
        $output .= "\n";

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

        return $output;
    }
}

1 Answer

Jamiu Oloyede
Jamiu Oloyede
345 Points

Hi ilanaguttman,

You need to echo the recipe object for the __toString() magic method to work.

          echo $recipe1;
          ```