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 Adding Getters and Setters

Little mistake on setSource()

The video shows

$recipe1->setSource = "Grandma Holligan";

It should be

$recipe1->setSource("Grandma Holligan");

As the value for setSource is considered invalid, the code reverts to the default value for the $source property defined on top of recipe.php, and that is the value shown on the console: Alena Holligan, instead of Grandma Holligan.

Yes, the setSource method should be using parenthesis.

geoffrey
geoffrey
28,736 Points

I know It's 5 month old, but maybe this can be changed (fixed), what do you think Alena Holligan ? Maybe a note could be added to the course about that mistake. This error indeed makes things confusing.

Thank you for this. I have no idea how she got it to work in the video. I couldn't get it to work unless I added the parens.

Fabrício Montenegro
Fabrício Montenegro
18,723 Points

I know this is old but I figured this information might help others.

Petrov von Petrov catched the mistake nicely! But the value for setSource is not considered invalid, this is why we get no errors. What is happening is a little messy. She ends up creating a property called setSource at runtime (I didn't even know this was possible in PHP) and making it equal to "Grandma Holligan".

Take a look at this example:

<?php
class MyClass
{
    public function myMethod()
    {
        return "I'm a method.";
    }
}

$myClassInstance = new MyClass();
$myClassInstance->myMethod = "I'm a string.";

var_dump($myClassInstance->myMethod);
var_dump($myClassInstance->myMethod());
?>

This piece of code returns:

string(13) "I'm a string."
string(13) "I'm a method."

Now, myClassInstance has both a method and a property called myMethod.

In the video, the class now has a method called setSource (which really sets the source), and also a property called setSource (which now contains the string passed). This means the property source remains unchanged from the default ("Alena Holligan"), as pointed out by Petrov von Petrov.

1 Answer

I noticed this, thank you for pointing it out for all to see