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

[Solved] PHP OOP basics mid course test confusion.

Working on the PHP Object Oriented Programming course and am at the mid-way test and on the final task of the test but I am getting lost on some logic when trying to print out the sentence.

See my getInfo return. What am I missing? I have an PHP IDE which says it's a valid return statement.

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

    function __construct($name, $flavor, $record){
        $this->common_name =$name;
        $this->flavor =$flavor;
        $this->record_weight =$record;
    }
    public function getInfo(){
        return "A ". $this->$common_name."is an ". $this->$flavor."flavored fish. The world weight is". $this->$record_weight;

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

echo $bass->getInfo();

hi Stephen

the problem is when you refer to $this within that class there is no need to access the classes property with the $ infront of the property name. the error is in your call to the getInfo() method. Instead of calling $this->$common_name call it like this $this->common_name. so your code should look like this.

php

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

    function __construct($name, $flavor, $record){
        $this->common_name =$name;
        $this->flavor =$flavor;
        $this->record_weight =$record;
    }
    public function getInfo(){
        return "A ". $this->common_name."is an ". $this->flavor."flavored fish. The world weight is". $this->record_weight;

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

echo $bass->getInfo();

'''

Andreas you are correct. Could you please repost your answer as an answer so it can be voted as the best?

2 Answers

php

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

    function __construct($name, $flavor, $record){
        $this->common_name =$name;
        $this->flavor =$flavor;
        $this->record_weight =$record;
    }
    public function getInfo(){
        return "A ". $this->common_name."is an ". $this->flavor."flavored fish. The world weight is". $this->record_weight;

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

echo $bass->getInfo();

'''

Guess you can say 'derp'. Thanks for that. Glad I was so close!