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

Aruna Srinivasiah
PLUS
Aruna Srinivasiah
Courses Plus Student 598 Points

How do I create an object with some data?

Does the name, flavor, and record go as arguments?

fish.php
<?php 

class Fish {
  public $common_name;
  public $flavor;
  public $record_weight;

}

$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces");

?>

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! No, the properties aren't set using an initializer so they do not go in as arguments. You will be accessing the properties of the new object directly. Let me see if I can give you another example which might allow you to extrapolate the answer.

<?php 

class Person {
  public $first_name;
  public $last_name;
  public $treehouse_student;

}

$jennifer = new Person();
$jennifer->first_name = "Jennifer"

?>

Each property will be accessed individually and set to the values corresponding to what is found in the challenge instructions. Good luck, but let me know if you're still stuck! :sparkles:

Hi Aruna, if you want to create an object like this you need to modify your class so it accepts arguments. The code should look like this:

<?php 

class Fish($name, $flavor, $record)
 {

  public $this->common_name = $name;
  public $this->flavor = $flavor;
  public $this->record_weight = $record;

}

$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces");

?>