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 trialKeegan Kemp
1,986 PointsUnexpected 'public' in Magic Methods, Object Oriented Php
Here's my code:
public function __toString()
{
return $this->getTitle();
}
echo $recipe1;
Any thoughts? It worked fine before I added this method. Now it displays an error even when I return to the previous echo:
echo Render::displayRecip$recipe1;
and remove the magic method __toString.
3 Answers
Justin Black
24,793 PointsYour function for setTitle is missing a bracket.
Your function:
public function setTitle($title)
{
if (empty($title)) {
$this->title = null;
} else {
$this->title = ucwords($title);
}
What it should be (excluding indentation):
public function setTitle($title)
{
if (empty($title)) {
$this->title = null;
} else {
$this->title = ucwords($title);
}
}
Keegan Kemp
1,986 PointsAh. I see that now. Thank you!
Rohald van Merode
Treehouse StaffHey Keegan Kemp 👋
It's hard to tell why this is happening with only seeing these snippets of code. The __toString
looks fine to me, but is most likely either misplaced in your Recipe
class or there might be a }
missing. Make sure that this snippet is placed directly in your Recipe
class and not nested elsewhere.
If that doesn't fix your issue please share a snapshot of your workspace so we can have a look at your complete code and I'd be more than happy to have another look 😃
Keegan Kemp
1,986 Points<?php
include "classes/recipes.php";
include "classes/render.php";
$recipe1 = new Recipe("my first recipe");
$recipe1->setSource = "Grandma Holligan";
$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;
And my cookbook.php file.
Keegan Kemp
1,986 PointsKeegan Kemp
1,986 Pointshttps://w.trhou.se/7s002r7xl4