PHP classes - extending the constructor function

Posted on 2011-04-28
Last Modified: 2012-05-11

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

Question by:QuintusSmit
    LVL 107

    Accepted Solution

    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.
    LVL 7

    Assisted Solution

    by:Vimal DM

    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)

    LVL 1

    Author Comment


    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Mobile App Development with PhoneGap

    PhoneGap can help you leverage your already existing HTML5, JavaScript, and CSS skills in order to create and deploy cross-platform mobile apps.This program comprehensively covers HTML5 mobile app development from top to bottom.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now