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 Extending Object-Oriented PHP Inheritance Extend a Class

Johann Wichtig
Johann Wichtig
560 Points

Extending Object oriented PHP: task 3 of 3, where am I going wrong?

The task is "Extending from a parent class allows the child to inherit all attributes and abilities of the parent.

Step 1: Add a new method to the Developer class named displayUserSkills.

Step 2: Use the 'name' from the PARENT class, and the 'skills' from the CURRENT class, to replace the <PLACEHOLDERS> and RETURN following string (NOTE that skills separated with a comma and space): <NAME> has the following skills: <SKILL1>, <SKILL2>, <SKILL3> Example Output: Alena has the following skills: PHP, MySQL, HTML"

I keep getting:

"Bummer! Incorrect output"

but when I check the output it comes up EXACTLY as it appears above. I can only assume that I'm arriving at the same conclusion while having missed a key step that they wanted me to use?

User.php
<?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;
    }
}
Developer.php
<?php

//add new child class here
class Developer extends User
{
    protected $skills = array();

    public function setSkills($array)
    {
      $this->skills = $array;
    }
    public function getSkills()
    {
      return $this->skills;
    }
    public function displayUserSkills()
    {
      return $this->getName() . " has the following skills: " . implode(", ", $this->getSkills()) . "."; 
    }
}

$example = new Developer();
$array = array("PHP", "MySQL", "HTML");
$example->setSkills($array);
$example->setName("Alena");
echo $example->displayUserSkills();

1 Answer

Joel Bardsley
Joel Bardsley
31,246 Points

Hi Johann, you've just slightly over-complicated this task - don't forget that the displayUserSkills method already has access to the $name and $skills properties, so there's no need to call the getter methods for each.

Also, the output they've provided is just an example - you don't need to set the properties to match. The task is only asking for the displayUserSkills method to be functional, which you've already done aside from the point I've mentioned above.

Edit - It may just be you need to remove the full-stop at the end of your returned string!

Johann Wichtig
Johann Wichtig
560 Points

Hi Joel, it was indeed just the full stop concatenated at the end that was making the output show up as incorrect. Thanks for coming to aid once again!