Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,721 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 ).