Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Object-Oriented PHP Basics (Retired) Properties and Methods Mid-Course Challenge

Eli Tamosauskas
Eli Tamosauskas
9,776 Points

Why is this not working

Why is this code not passing? Can anyone see?

fish.php
<?php

class Fish {

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

  public function __construct($name, $flavor, $record) {
    $this->common_name = $name;
    $this->flavor = $flavor;
    $this->record_weight = $record;
  }

    //methods

  public function getInfo () {
    echo "A " . $name . " is an " . $flavor . " flavoured fish. The world record weight is " . $weight . ".";  
  };
}

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


getInfo($bass);

?>

3 Answers

echo function should take a string. So just wrap your text after 'echo' into a ' ' or " "

upd: you should call 'getInfo' as a method of the object called $bass like: $bass->getInfo();

Eli Tamosauskas
Eli Tamosauskas
9,776 Points

But my echo text IS wrapped, look at example above? Also tried your method calling didnt solve.

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

Hey Eli,

There's a couple things that are going on here as to why your code is not passing. I'll try to explain classes the best way I can to see if you can figure out why your code doesn't work.

We'll start with this Fish class example and only use 1 property for the class, "common name".

<?php

class Fish {

    public $common_name;
}

This is giving the class Fish a property called $common_name. Now we'll add the construct method:

<?php

class Fish {

    public $common_name;

    public function __construct($name) {
        $this->common_name = $name;
    }
}

The construct method runs once you create a new instance of the class. Now lets create a 2 new instances of this class.

<?php

class Fish {

    public $common_name;

    public function __construct($name) {
        $this->common_name = $name;
    }
}

$fish_one = new Fish("Eli");
$fish_two = new Fish("Mike");

We now have TWO fish objects, each with different names. Notice how I created the objects outside of the class. If you want to access the property of $common_name on either object, you can call it based off of the object you created.

echo $fish_one->common_name; // Outputs: "Eli"
echo $fish_two->common_name; // Outputs: "Mike"

If you want to create a method in the class that returns the name for you, you would use the $this keyword inside of the class and its methods. Let's create 2 methods called getCommonName and greeting and then see how they're used.

<?php

class Fish {

    public $common_name;

    public function __construct($name) {
        $this->common_name = $name;
    }

   public function getCommonName(){
        return $this->common_name;
   }

    public function greeting(){
         return "Good day, " . $this->common_name . "!";
    }
}

$fish_one = new Fish("Eli");
$fish_two = new Fish("Mike");
echo $fish_one->getCommonName(); // Outputs: "Eli"
echo $fish_one->greeting(); // Outputs: "Good day, Eli!"

echo $fish_two->getCommonName(); // Outputs: "Mike"
echo $fish_two->greeting(); // Outputs: "Good day, Mike!"
Eli Tamosauskas
Eli Tamosauskas
9,776 Points

Thats a very nice explanation, however that didnt solve my code problem. I understood the concept just fine i think. Code still not passing though

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

Eli Tamosauskas Would you mind posting your new modified code here so I can see where its going wrong?