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 trialMohammed Jammeh
37,463 PointsExtending Constructor Method from Parent Class in OOP
Hi, I have been struggling with this for a while and any help will be useful. The instruction states: 'Within the Client class, extend the constructor method from the parent to allow the company to be included with initialization.'
<?php
//do not modify this file
class User {
protected $name;
protected $title;
public function __construct($name = null, $title = null) {
$this->name = $name;
$this->title = $title;
}
public function __toString() {
return $this->getSalutation();
}
public function getSalutation() {
return $this->title . " " . $this->name;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getTitle() {
return $this->title;
}
public function setTitle($title) {
$this->title = $title;
}
}
<?php
//do not modify this file
class Developer extends User
{
protected $skills;
public function getSkills() {
return $this->skills;
}
public function setSkills($array) {
$this->skills = $array;
}
public function displayUserSkills() {
return $this->getName() . ' has the following skills: ' . implode(", ", $this->skills);
}
}
This is my solution below but it keeps saying the company is not set and if I remove the other parameters, it does not work either.
<?php
class Client extends User {
protected $company;
public function getCompany () {
return $this->company;
}
public function setCompany ($company) {
return $this->company = $company;
}
public function __construct ($name, $title, $company) {
parent::__construct($name, $title, $company);
}
I'm quite new to OOP and any help will be very useful. Thank you :)
2 Answers
Justin Radcliffe
18,987 PointsHi Mohammed,
You don't need to pass $company as a parameter to the User (parent) Class constructor method as it is a unique property of Client Class. Then you need to call the setCompany method on the (current) object ($this) of the Client Class. The following code worked for me:
<?php
//add code for Client class
class Client extends User
{
protected $company;
public function __construct($name = null, $title = null, $company = null)
{
parent::__construct($name, $title);
$this->setCompany($company);
}
public function getCompany() {
return $this->company;
}
public function setCompany($array) {
$this->company = $array;
}
}
Alexandru Palita
14,261 PointsAdd
<? php
public function __construct ($name, $title, $company) {
parent::__construct($name, $title);
$this->company = $company;
}
Mohammed Jammeh
37,463 PointsMohammed Jammeh
37,463 PointsThank you very much for your answer, Junstin. It's very helpful. The fact that I can't pass the $company parameter to the User Class does make sense. I've been struggling with it for a while now haha but thank you :)