2

guys! I am practising class extending now but I think I really miss something.

I have some variables first:

$username = "antonradev";
$name = "Anton Radev";
$email = "antonradev@example.com";
$profession = "Designer";
$job_title = "Web Design Manager";
$job_location = "Sofia";

My parent class:

class User {
    public $username;
    public $name;
    public $email;

    public function __construct($username, $name, $email) {
        $this->username = $username;
        $this->name = $name;
        $this->email = $email;
    }

}

After this I extend like this:

class User_Professional extends User {

    public $user_profession;
    public $user_job_title;
    public $user_work_location;

    public function __construct($user_profession, $user_job_title, $user_work_location) {
        $this->user_profession = $user_profession;
        $this->user_job_title = $user_job_title;
        $this->user_work_location = $user_work_location;
    }

}

I create an instance:

$user_professional = new User_Professional($username, $name, $email, $profession, $job_title, $job_location);

And I am trying to print some data:

print "The employee username is: " . $user_professional->username;

But nothing happen. Its empty with no errors:

The employee username is:

Then I make some changes and I am trying to print other property:

print "The employee`s job title is: " . $user_professional->user_job_title;

But I get data from the parent class. It prints wrong property:

The employee`s job title is: Anton Radev

Is this normal? Where is my mistake? I cannot handle it. Thank you!

Liam James
  • 414
  • 1
  • 6
  • 15

4 Answers4

2

You must call the constructor of User class and pass 3 arguments there $username, $name, $email.

User class constructor:

public function __construct($username, $name, $email) {
    $this->username = $username;
    $this->name = $name;
    $this->email = $email;
}

User_Professional class constructor:

public function __construct($username, $name, $email, $user_profession, $user_job_title, $user_work_location) {
    parent::__construct($username, $name, $email);

    $this->user_profession = $user_profession;
    $this->user_job_title = $user_job_title;
    $this->user_work_location = $user_work_location;
}
sybear
  • 7,837
  • 1
  • 22
  • 38
  • Oh! I did not know this.. Do I have to include with this parent:: in every extended class the initial parent functions? Is this valid only for the constructors? Thank you! – Liam James Feb 13 '14 at 09:00
  • Usually you should always call `parent::__construct()` in child classes constructors. However, parent's constructor is automatically called if you DO NOT explicitly define constructor in your child class. More info: http://www.php.net/manual/en/language.oop5.decon.php Also, dont forget mark my answer as accepted, if you find it helpful :) – sybear Feb 13 '14 at 09:47
1
class User_Professional extends User {

    public $user_profession;
    public $user_job_title;
    public $user_work_location;

    public function __construct($user_profession, $user_job_title, $user_work_location) {

        // Need to pass the usename, name, email
        parent::__construct($username, $name, $email);

        $this->user_profession = $user_profession;
        $this->user_job_title = $user_job_title;
        $this->user_work_location = $user_work_location;
    }

}

Alternate

class User {
    public $username;
    public $name;
    public $email;

    public function __construct() {
        $this->username = "antonradev";
        $this->name = "Anton Radev";
        $this->email = "antonradev@example.com";
    }

}

And use in your child class like

class User_Professional extends User {

    public $user_profession;
    public $user_job_title;
    public $user_work_location;

    public function __construct($user_profession, $user_job_title, $user_work_location) {

        parent::__construct();

        $this->user_profession = $user_profession;
        $this->user_job_title = $user_job_title;
        $this->user_work_location = $user_work_location;
    }

}
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • Hi! Thank you! I added this line (parent::__construct($username, $name, $email);) and I get three errors about "Notice: Undefined variable..." – Liam James Feb 13 '14 at 09:14
  • @AntonRadev this is because you have to pass the `$username, $name, $email` to the `parent::__construct($username, $name, $email)` to initiate your parent class – jogesh_pi Feb 13 '14 at 09:19
  • Yes. Now my child class constructor is public function __construct($username, $name, $email, $user_profession, $user_job_title, $user_work_location). It seems lot of properties adding all the time everywhere. :( What is the use of the extending then. I dont get it again.. – Liam James Feb 13 '14 at 09:28
0

You are overriding your parents constructor. You need to call it.

class User_Professional extends User {

  public $user_profession;
  public $user_job_title;
  public $user_work_location;

  public function __construct($user_profession, $user_job_title, $user_work_location) {
    parent::__constructor($username, $name, $email);

    $this->user_profession = $user_profession;
    $this->user_job_title = $user_job_title;
    $this->user_work_location = $user_work_location;
  }
}
realshadow
  • 2,599
  • 1
  • 20
  • 38
  • Overriding, not overwriting. – Rob Baillie Feb 13 '14 at 08:59
  • Hm.. It seems like double job. I have to include every time the parent properties and methods. But what If I do not know them? I thought inheritance is a way of working with child class without thinking about the parent one. I have gotten the ideas wrong? – Liam James Feb 13 '14 at 09:03
0

You should use composition or mixin instead of inheritance in this case.

Community
  • 1
  • 1
Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • Hi, Ostrovski! Thank you! I am not that far in the book :) And I think it is not mentioned as a topic. What is the different in this case? Isn`t inheritance the way to give a child class the initial properties of the parent one and the child class gets more properties we can use? – Liam James Feb 13 '14 at 08:58
  • You are right ofcourse and my answer is just a little offtopic. But when you will need another one User* class like User_Father and then another one like User_Politician you will make all available combination of such classes (i.e. User_ProfessionalAndPolititian, User_FaterAndPolititian) to obtain all desired properties in some place in code. Such combinatoric growth of new classes can be avoided with composition or mixin technics. – Ivan Velichko Feb 13 '14 at 10:54