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 trialMichael Duncan
1,992 PointsPHP: 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; } } ?>
7 Answers
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");
}
}
Elijah Collins
19,457 PointsThis might help
<?php
class Trout extends Fish {
function __construct($name, $flavor, $record) {
parent::__construct($name, $flavor, $record);
}
?>
Jeremiah Wodke
3,192 Pointsthis works for step 2:
class Trout extends Fish {
function __construct($name, $flavor, $record) {
parent::__construct($name, $flavor, $record);
}
Austin Lilley
5,727 PointsI 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.
MUZ140592 Victor Mutasa
4,260 Pointsand t wont work
Jordan Pierre
16,601 PointsI 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
4,916 PointsFor me, there was a missing curly brace:
class Trout extends Fish { function __construct($name, $flavor, $record){ parent:: __construct($name, $flavor, $record); } }
Michael Duncan
1,992 PointsMichael Duncan
1,992 PointsIt is not formatting my code properly. Sorry