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) Inheritance, Interfaces, and Exceptions Final Challenge

Michael Duncan
Michael Duncan
1,992 Points

PHP: Calling a parent constructor?

I am having a difficult time understanding the purpose for the construct. In the code challenge, i am asked to "Create a constructor for the Trout class with three parameters in order: $name, $flavor, and $record. Call the parent constructor inside the new constructor."

HERE IS MY CODE: Can anyone guide me in the right direction to comprehend this concept?

<?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() {
    $output  = "The {$this->common_name} is an awesome fish. ";
    $output .= "It is very {$this->flavor} when eaten. ";
    $output .= "Currently the world record {$this->common_name} weighed {$this->record_weight}.";
    return $output;
}

}

class Trout extends Fish { function construct($name, $flavor, $record){ parent::construct(); $this->common_name = $name; $this->flavor = $flavor; $this->record_weight = $record; } } ?>

Michael Duncan
Michael Duncan
1,992 Points

It is not formatting my code properly. Sorry

7 Answers

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points
<?php

class Trout {


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

}
?>


class TroutExtra extends Trout {

  function __construct(){
     parent::__construct("name","flavour","record");
  }

}

This might help

<?php
class Trout extends Fish {
  function __construct($name, $flavor, $record) {
  parent::__construct($name, $flavor, $record);
  }
?>
Jeremiah Wodke
Jeremiah Wodke
3,192 Points

this works for step 2:

class Trout extends Fish { function __construct($name, $flavor, $record) { parent::__construct($name, $flavor, $record); }

I too am stuck on this, I tried both of the above and they didn't seem to help. Constructors seem to be a bit confusing for me. I've completed step 1 of this challenge without issues, but I am stuck on step 2.

and t wont work

Jordan Pierre
Jordan Pierre
16,601 Points

I am stuck at this part also. Here is my code.

class Trout extends Fish { function_construct($name, $flavor, $record){ parent::_construct($name, $flavor, $record); } }

It looks like this has worked for other people but it is not working for me. What am I doing wrong?

Ilonka Hofmann
Ilonka Hofmann
4,916 Points

For me, there was a missing curly brace:

class Trout extends Fish { function __construct($name, $flavor, $record){ parent:: __construct($name, $flavor, $record); } }