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!

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

Sam Katz
Sam Katz
2,986 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: