PHP Object-Oriented PHP Basics Understanding Classes Properties of a Class

Sam Katz
Sam Katz
2,602 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.

fish.php
<?php 

class Fish {

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


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


?>

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
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: