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 trialClaudio Lacayo
671 PointsWhy am I not able to assign parameters to variables in constructor?
<code> <?php
class Fish { public $common_name; public $flavor; public $record_weight; function __construct($name, $flavor, $record) { $this->name = $common_name; $this->flavor = $flavor; $this->record_weight = $record_weight;
} } ?> </code>
I'm not sure but this seems like a bug. Task 2 requires you to submit the parameter as $name, but then Task 3 requires that you refer to it as common_name? If I use the code:
<code> $this->common_name = $common_name; </code>
It appears to work but then requires me to go back to Task 2. If anyone could provide some input it would be greatly appreciated!
1 Answer
Winnie E Tibingana
21,066 PointsHi Claudio,
the way you are defining the constructor is correct. the only problem is with the assignment of the constructor parameters the constructor will use.
function __construct($name, $flavor, $record) {
$this->name = $common_name; //this should be $this->common_name = $name;
$this->flavor = $flavor;
$this->record_weight = $record_weight; // this is not correct either. Please correct as explained above.
}
Constructors allow you to initialise your object's properties when you instantiate (create) an object. Please read more here: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php and here: http://php.net/manual/en/oop4.constructor.php
Your object is the Fish
class.
We then provide each constructor parameter ($name, $flavor, $record) a value in relation to the class using $this
which is a reference to the current object Fish
and the arrow operator (->) as below:
$this->common_name = $name;
Hope this helps.