QuintusSmit
asked on
PHP classes - extending the constructor function
Hi
I have a php class called user. The constructor function takes variables and assign them.
I created this as a generic class and wish to use it to keep track of my students. Can I extend it to a new class called Students and extend the constructor function of the base class to now include a new variable called student_number?
Here is the current class:
I have a php class called user. The constructor function takes variables and assign them.
I created this as a generic class and wish to use it to keep track of my students. Can I extend it to a new class called Students and extend the constructor function of the base class to now include a new variable called student_number?
Here is the current class:
class user{
function user($type, $id, $name, $middlename, $surname, $cell_no, $tel_no, $email, $phys_addr1, $phys_addr2,
$phys_addr3, $phys_city, $phys_state, $phys_code, $phys_country, $postal_addr1, $postal_addr2, $postal_addr3, $postal_city,
$postal_state, $postal_code, $postal_country){
$this->type = $type;
$this->id = $id;
$this->name = $name;
$this->surname = $surname;
$this->middlename = $middlename;
$this->cell_no = $cell_no;
$this->tel_no = $tel_no;
$this->email = $email;
$this->phys_addr1 = $phys_addr1;
$this->phys_addr2 = $phys_addr2;
$this->phys_addr3 = $phys_addr3;
$this->phys_city = $phys_city;
$this->phys_state = $phys_state;
$this->phys_code = $phys_code;
$this->phys_country = $phys_country;
$this->postal_addr1= $postal_addr1;
$this->postal_addr2 = $postal_addr2;
$this->postal_addr3 = $postal_addr3;
$this->postal_city = $postal_city;
$this->postal_state = $postal_state;
$this->postal_code = $postal_code;
$this->postal_country = $postal_country;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for the responses. I will only be able to look at it again this weekend as this is just a hobby.
@ray
To clear up the question I had.
If a base/parent class constructor function accepts arguments x and y for example
base class
constructor function(x,y){
assign x to class variable
assign y to class variable
}
and I now extend the class but want to add new variable z to the constructor function:
child class extends base class
constructor function(x,y, z){
assign x to class variable
assign y to class variable
assign z to class variable
}
do I write the whole constructor function from scratch again like above for the new extended class, in which case what is the point of extending if I have to rewrite the whole thing or is there a sneaky way to just add z as well to the original function?