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 Understanding Classes Properties of a Class

Sam Katz
Sam Katz
2,924 Points

I'm not sure how to set the properties in a new instance/object in Object Oriented PHP basics

I'd rather understand OOP in PHP rather than use example code.


class Fish {

  public $common_name;
  public $flavor;
  public $record_weight;

$bass = new Fish();
$bass->common_name = "Largemouth Bass";


1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Sam Katz! First, you're doing fine. You set the common name, but didn't set the rest of the properties yet. Challenges are meant to be a check to make sure that you are understanding everything so far. As such, they tend to be very small code snippets just to make sure you can do the things being asked in that challenge. As you progress through the course, your understanding of OOP will deepen and the course material will get more in-depth as well. Just continue along the path that you're on.

$bass->common_name = "Largemouth Bass";  // you only got to here
$bass->flavor = "Excellent";
$bass->record_weight = "22 pounds 5 ounces";

The above sets the properties of the Fish() you created. As you move forward, you will learn how to set these at the time the instance is created through the use of a constructor.

Hope this helps! :sparkles: