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

Help with php class extends

Can anyone see an error with this code ? name works but userAge doesn't.

class age extends user{

    public $userAge;

        function __construct($name, $password, $ocupation, $userAge){

        parent:: __construct($name, $password, $ocupation); 

            $this->userAge = $userAge;

    }

    public function getInfo(){

        echo $this->name;
        echo $this->userAge;

    }

}

1 Answer

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

Adding the user class seems to have everything working for me. Not sure what the user class was but I created one on the fly.

class user {
    public $name;
    public $password;
    public $occupation;

    public function __construct($name, $password, $occupation){
        $this->name = $name;
        $this->password = $password;
        $this->occupation = $occupation;
    }

}


class age extends user{

    public $userAge;

    function __construct($name, $password, $occupation, $userAge){
        parent:: __construct($name, $password, $occupation); 

        $this->userAge = $userAge;
    }

    public function getInfo(){
        echo "Name: " . $this->name . " - Age: " . $this->userAge;
    }
}

$user = new age('name', 'password', 'job', 120);
$user->getInfo();