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 Understanding Classes Accessing Methods

The code should display recipe_1 and recipe_2. But somehow it doesn't.. Can someone review my code?

class Recipe // Creates Class Recipe { public $title; // Creates public property from class Recipe with a property named title with no default value. public $ingredients = array(); // Creates public array property from class Recipe named ingredients. public $instructions = array(); public $yield; public $tag = array(); public $source = "Alena Holligan "; // Creates public property from class Recipe named source with a default value "Alena Holligan".

public function displayRecipe()
{
    $this -> title . " by " . $this -> source;
}

}

$recipe_1 = new Recipe(); // Creates an object named recipe_1 $recipe_1 -> source = "Grandma Holligan "; // Sets the property of source to "Grandma Holligan". $recipe_1 -> title = "My first recipe";

$recipe_2 = new Recipe(); // Creates an object named recipe_2 $recipe_2 -> source = "Betty Crocker"; $recipe_2 -> title = "My second Recipe";

echo $recipe_1->displayRecipe(); echo $recipe_2 -> displayRecipe();

2 Answers

Skimming quickly through the code you never used the keyword return. It should look like this....

public function displayRecipe() 
    {
        return $this->title . " by " . $this->source;
    }

You should format the code in a better and more understandable way.

Sorry, my bad, It didn't copy-paste very well.. But I already solved it.