Constructors are special member functions for initial settings of newly created object instances from a class.
In PHP, a constructor is a method named __construct(), which is called by the keyword new after creating the object. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor arguments for the parameters.
Constructors are the very basic building blocks that define the future object and its nature. You can say that the Constructors are the blueprints for object creation providing values for member functions and member variables.
Constructor types:
- Default Constructor: It has no parameters, but the values to the default constructor can be passed dynamically.
- Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.
- Copy Constructor: It accepts the address of the other objects as a parameter.
Note: PHP lacks support for declaring multiple constructors of different numbers of parameters for a class unlike languages such as Java.
Multiple Constructors: More than one constructor in a single class for initializing instances are available.
Example 1: In the following example, we create a script and try to declare multiple constructors. We will assign the name of a student using one constructor and age with another constructor.
PHP
<?php class Student { // Constructor with 1 parameter public function __construct(string $name ) { } // Constructor with 2 parameters public function __construct(string $name , string $age ) { } } $obj1 = new Student( 'Akshit' ); $obj2 = new Student( 'Akshit' , '12' ); ?> |
Output:
PHP Fatal error: Cannot redeclare Student::__construct() in /home/33a7c36527d199adf721ab261035d4f7.php on line 10
We have to use different methods to use Multiple Constructors in PHP. Some are listed below.
Approach:
- Get all the assigned arguments by using PHP func_get_arg() to get a mentioned value from the argument passed as the parameters.
- We will first count the number of arguments passed to the constructor by using PHP func_num_args().
- By knowing type of arguments using PHP gettype() or data-type specific function like is_int().
- Name your method as per the number of arguments and check its existence using PHP method_exists().
- Call the suitable function using call_user_func_array() by passing the original arguments as call_user_func_array()’s second argument.
Example 2:
PHP
<?php class Student { public function ConstructorWithArgument1( $arg1 ) { echo ( 'Constructor with 1 parameter called: ' . $arg1 ). "<br>" ; } public function ConstructorWithArgument2( $arg1 , $arg2 ) { echo ('Constructor with 2 parameters called: '.$arg1.' ,'. $arg2 ). "<br>" ; } public function ConstructorWithArgument3( $arg1 , $arg2 , $arg3 ) { echo ('Constructor with 3 parameters called: '.$arg1.' , '.$arg2.' ,'. $arg3 ). "<br>" ; } public function __construct() { $arguments = func_get_args(); $numberOfArguments = func_num_args(); if (method_exists( $this , $function = 'ConstructorWithArgument' . $numberOfArguments )) { call_user_func_array( array ( $this , $function ), $arguments ); } } } // Constructor with 1 parameter $obj = new Student( 'Akshit' ); // Constructor with 2 parameters $obj = new Student( 'Akshit' , 'Nikita' ); // Constructor with 3 parameters $obj = new Student( 'Akshit' , 'Nikita' , 'Ritesh' ); ?> |
Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh
Approach:
- Check the type of argument that is passed with the constructor. It can be done using gettype() or data-type specific function like is_int().
- Determine the type and initialize the instance variable.
Example 3: The following example demonstrates multiple constructors with different data types like string or integer.
PHP
<?php class Student { public $id ; public $name ; // etc. public function __construct( $idOrName ) { if ( is_int ( $idOrName )) { $this ->id= $idOrName ; echo "ID is initialized using constructor" . "<br>" ; // other members are still uninitialized } else { $this ->name = $idOrName ; echo "Name is initialized using constructor" . "<br>" ; } } public function setID( $id ) { $this ->id = $id ; } public function setName( $name ) { $this ->name = $name ; } public function getinfo() { echo "ID : " . $this ->id. "<br>" ; echo "Name : " . $this ->name. "<br>" ; } } // Create instance $student = new Student( "Akshit" ); $student ->setID(1); $student ->getinfo(); $student2 = new Student(2); $student2 ->setName( "Nikita" ); $student2 ->getinfo(); ?> |
Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : Nikita