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

Clarification needed: why create a separate class with a static method for displaying the recipe?

I don't fully understand Alena's explanation about why its a good idea to create a separate class and create a static method on that class to display the recipe. Could someone explain this from a slightly different angle? Thanks.

1 Answer

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

The idea is that the Recipe class is only responsible for storing and managing information about the recipe, but not for how this information should be formatted and showed.

Imagine a scenario where you and I have access to this class, but we want to use it differently in our own applications. I might want to show the source of the recipe as a bold text while you don't want to use the source at all and just print the title as a header. So you would have one Render class while I would have a different one.

You might even want to show this information differently depending on where in your application the user is. So you could have many different Render classes, each one responsible for showing the information in a different way.

The styling of the information shouldn't be a concern of the Recipe class, this is not its job.

The class could have a default display method, but I believe Alena is using a different class to showcase the importance of keeping this aspect of the code in a separate class. (Also a good opportunity to explain the usage of static methods :D ).