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! While you're at it, check out some resources Treehouse students have shared here.

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

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:

Martin Balon
Martin Balon
43,651 Points

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");

?>