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!
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;
}
}
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;
}
}
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;
}
}
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;
}
You should use composition or mixin instead of inheritance in this case.