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 (Retired) Properties and Methods Mid-Course Challenge

stage 2 challenge 1

Add the following public properties to the Fish class: common_name, flavor, and record_weight.

fish.php
<?php

class Fish {
$p-> name = ("common_name"."flavor"."record_weight");
}

?>

2 Answers

Logan R
Logan R
22,989 Points

You create a public variable by doing:

public $variable_name;

So in this case, it would be:

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

When you want the property, like how you did, you do the following outside of the class:

$fish = new Fish();
$fish->common_name = "Fish";
Kristi Smythe
Kristi Smythe
10,665 Points

Hi Taf (pls forgive if this short name isn't ok ;)

The clues in this challenge are 'public' (the encapsulation access definition) and 'properties' (variables). So we are setting up our variables (properties) like so:

class Fish { public $property(first variable name) public $property(second variable name) public $property(third variable name) }

Defining these properties as public in our Fish class will enable us to utilize them for any object of fish that we instantiate (create) for different species of fish.

I'm also in always learning mode, so I hope this helps.

Cheers, Kristi