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

[SOLVED] Nothing Appears

So as I work on this section of the cookbook, I'm trying to run php cookbook.php but whenever I run it, instead of displaying a list of recipe titles it displays nothing at all.

I know its something on my end but I can't seem to pinpoint it. I keep going over the video and I'm confused as hell at this point. The download of the work is also different from what is covered in the video so again this is basically a wild goose chase without you fine folk in the forums.

Thank you in advance to whoever helps me!

classes/cookbook.php

<?php
include "classes/recipes.php";
include "classes/render.php";
include "classes/recipecollection.php";
include "inc/recipes.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()); 

//echo Render::displayRecipe($belgian_waffles);

classes/recipes.php

<?php

class Recipe 
{
    private $title;
    private $ingredients = array();
    private $instructions = array();
    private $yield;
    private $tag = array();
    private $source = "Alena Holligan";

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

    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 line " . __LINE__ . " in method " . __METHOD__;
        $output .= "\nThe following methods are available for objects of this class: \n";
        $output .= implode("\n", get_class_methods(__CLASS__));
        return $output;
    }
    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("PLeasue 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;
    }


}

classes/render.php

<?php

class Render
{
        public function __toString()
        {
            $output = "The following methods are available for " . __CLASS__ . "objects of this class: \n";
            $output .= implode("\n", get_class_methods(__CLASS__));
            return $output;
        }

        public static function listRecipes($titles)
        {
            asort($titles);
            return implode("\n", $titles);
        }

        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;
    }  
}

recipecollection.php

<?php

class RecipeCollection
{
    private $title;
    private $recipe = 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) {
          $title[] = $recipe->getTitle();
        }
        return $titles;
    }

}

Found my own error, was missing the 's' in $titles[] = $recipe->getTitle();

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