Monday 24 May 2021

Php: Why to use array as an argument to constructor definition

If your constructor takes more arguments, then passing an array will make it more readable. Let us see it with an example.

 

Let’s take an Employee class with 8 properties like id, name, age etc.,

class Employee{
    public $id;
    public $name;
    public $age;
    public $street;
    public $city;
    public $state;
    public $country;
    public $pin;

    public function __construct($id, $name, $age, $street, $city, $state, $country, $pin){
        $this->id = $id;
        $this->name = $name;
        $this->age = $age;
        $this->street = $street;
        $this->city = $city;
        $this->state = $state;
        $this->country = $country;
        $this->pin = $pin;
    }

}

 

As you see the constructor definition, it is taking 8 argument to initialize Employee instance. What if you need to initialize 100 properties of an object at the time of creation, how will you remember the order of argument, it is quite impossible and leads to erroneous code. But we can solve this problem by passing an array as argument to the constructor.

public function __construct($arr=[]){
    $this->id = $arr['id'];
    $this->name = $arr['name'];
    $this->age = $arr['age'];
    $this->street = $arr['street'];
    $this->city = $arr['city'];
    $this->state = $arr['state'];
    $this->country = $arr['country'];
    $this->pin = $arr['pin'];
}

 

You can define an Employee object like below.

$emp1 = new Employee(
    [
        'id'=>1, 
        'name'=>'Krishna', 
        'age'=>31, 
        'street'=>'Chamundeswari street', 
        'city'=>'Bangalore', 
        'state'=>'Karnataka', 
        'country'=>'India', 
        'pin'=>'560032'
    ]
);


Find the below working application.

 

constructor_array_argument_demo.php

#!/usr/bin/php

<?php

    class Employee{
        public $id;
        public $name;
        public $age;
        public $street;
        public $city;
        public $state;
        public $country;
        public $pin;

        public function __construct($arr=[]){
            $this->id = $arr['id'];
            $this->name = $arr['name'];
            $this->age = $arr['age'];
            $this->street = $arr['street'];
            $this->city = $arr['city'];
            $this->state = $arr['state'];
            $this->country = $arr['country'];
            $this->pin = $arr['pin'];
        }

        public function __toString(){
            return "id : $this->id\n"
            . "name : $this->name\n"
            . "age: $this->age\n"
            . "street : $this->street\n"
            ."city : $this->city\n"
            . "state : $this->state\n"
            ."country : $this->country\n"
            ."pin: $this->pin\n";
        }
    }

    $emp1 = new Employee(
        [
            'id'=>1, 
            'name'=>'Krishna', 
            'age'=>31, 
            'street'=>'Chamundeswari street', 
            'city'=>'Bangalore', 
            'state'=>'Karnataka', 
            'country'=>'India', 
            'pin'=>'560032']
        );
    $info = (string)$emp1;

    echo $info;

?>


Output

$./constructor_array_argument_demo.php 

id : 1
name : Krishna
age: 31
street : Chamundeswari street
city : Bangalore
state : Karnataka
country : India
pin: 560032







 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment