Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter for parent::__constructor() function while instantiating an object in php

Tags:

oop

php

Hello everybody I'm very new to Object Oriented Programming in php. So sometimes i need help while learning OOP in PHP. Here's a class named parentClass i have with a few method and properties like:

    class parentClass 
    {
    public $fname;
    public $lname;
     public $age;

    function __construct($title, $firstName, $lastName, $age,$address)  
      {
        $this->title    = $title;
        $this->fname    = $firstName;
        $this->lname   = $lastName;
        $this->age      = $age;
        $this->address = $address;

      }



     public function getFullName()
    {
        return $this->fname . " " . $this->lname. " ". $this->address;
     }
    }

I also have a child class named childClass which extends parentClass with few another properties and methods and also have another _constructor function in it. And then I Called parentClass's method and properties with parent::__construct

    class childClass extends parentClass
    {

    function __construct($title1, $firstName1, $mainName1, $age1)   
    {
     parent::__construct($title, $firstName, $lastName, $age, $address)
        $this->title1   = $title1;
        $this->fname1   = $firstName1;
        $this->lname1   = $mainName1;
        $this->age1     = $age1;

     }

    function getFullName1()
    {
        return $this->fname1 . " " . $this->lname1 . " ". $this->address;
     }
    }

So now I have to pass argument to parentClass's construcotr function's parameter. And I want getFullName1 method to return data with address. if I write:

         $obj = new childClass("title", "Rohim", "Uddin", 50);

Here i can pass argument in only childClass's __constructor function parameter.

My question is where to pass argument for parentClass's construcotr function's parameter? more specific for $address parameter; Thanks in advance. And I'm sorry for any mistake.

like image 696
Nirob Avatar asked Oct 31 '25 15:10

Nirob


1 Answers

First a little explaination of what inheritence means. The fact here is that childClass is an instance of parentClass. So what does that mean? That means that childClass can use all the variables and methods of parentClass as long the access modifier is public or protected. You choosed the create public variables, thus that mean that you can use the variables defined in parentClass in your childClass. That means this will work perfecly fine:

$instance = new childClass("Title","Firstname","Lastname",etc.);
echo $instance->fname; //echo's "Firstname" (according to the code below)   

If you use inheritance, the child constructor need to receive the parameters you want to pass through the parent's constructor.

Your almost there, i putted some comments in the code below:

class parentClass 
{
    //Create the title variable if you want to use it here
    public $title;
    public $fname;
    public $lname;
    public $age;
    //Create the address variable if you want to use it here
    public $address;

    function __construct($title, $firstName, $lastName, $age,$address)  
    {
        $this->title    = $title;
        $this->fname    = $firstName;
        $this->lname    = $lastName;
        $this->age      = $age;
        $this->address  = $address;

    }

    public function getFullName()
    {
        return $this->fname . " " . $this->lname. " ". $this->address;
    }
}

class childClass extends parentClass
{

    //It's not necessary to add an `1` or something else to this parameters. Just give them clean names.
    //If you want to pass the `$address` variable through the constructor you should add this parameter to this constructor. Since it's the constructor the variable won't be filled elsewhere.
    function __construct($title, $firstName, $lastName, $age, $address)   
    {
        parent::__construct($title, $firstName, $lastName, $age, $address)

        //This code is too much. In the line above you already passed the variables to the parent constructor. There they are handled by the parents constructor. 
        //As i explained above you can access these variables, thus it makes no sence to create new variables here or to override the just-setted variables.
        /*$this->title1   = $title1;
        $this->fname1   = $firstName1;
        $this->lname1   = $mainName1;
        $this->age1     = $age1;*/
    }

    //Why you write this function here? Because `childClass` inherits from `parentClass` you can just use the methods of `parentClass`. If you call `childClass->getFullName()` the `getFullName` function in `parentClass` will be executed.
    //It will return the same values as you are returning here. Just leave this function. Only create this function if you want to override the default `parentClass->getFullName` behaviour. Then leave the 1 so it will be `getFullName` instead of `getFullName1`
    /*function getFullName1()
    {
        return $this->fname1 . " " . $this->lname1 . " ". $this->address;
    }*/
}

So what's happening at the end?

$instance = new childClass("My Title", "Stefan", "Pols", 25, "My Adress 1");
//`childClass` constructor is called here.
//The first line (`parent::__construct`) will pass the variables to the parentClass. There they are set to the right variables.

echo $instance->getFullName();
//Compiler tries to find the method `getFullName()` in `childClass`. It doesn't exist.
//Compiler sees it inherits from `parentClass` so it's going to search the method `getFullName()` in `parentClass`
//It does exist. Since we setted the variables during the creation of the `$instance` object (through the constructor) this function will return the right values.

Output: `$this->fname . " " . $this->lname. " ". $this->address;`  > Stefan Pols My Adress 1
like image 190
S.Pols Avatar answered Nov 03 '25 06:11

S.Pols



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!