PHP classes - extending the constructor function


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;

Open in new window

Who is Participating?
Ray PaseurConnect With a Mentor Commented:
Hmm... Not sure I understand this question completely, but in any case, you want to move to PHP5 programming standards and that means naming the constructor according to the OOP5 standards.  See these pages for more:

Typically, you want to use "getter" and "setter" methods for adding or extracting information in your class properties.  In the case of what I think you are doing with the User class, you might want to let the user definition be an associative array (sort of an active-record design pattern).  Without something like that, you would have a lot of individual named variables, kind of hard to keep track of.

If you want to extend the User class to Student, you can let Student call the parent constructor, then call its own constructor to load the student_number.
Vimal DMConnect With a Mentor Commented:

Look at the below samples and make your code accordingly

class Named_Cart extends Cart {
    var $owner;
    function set_owner ($name) {
        $this->owner = $name;


$ncart = new Named_Cart;    // Create a named cart
$ncart->set_owner("kris");  // Name that cart
print $ncart->owner;        // print the cart owners name
$ncart->add_item("10", 1);  // (inherited functionality from cart)

QuintusSmitAuthor Commented:

Thank you all for the responses. I will only be able to look at it again this weekend as this is just a hobby.

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?
All Courses

From novice to tech pro — start learning today.