Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP

This post provides constructor example in PHP and quick solution of the below warning you may be facing during the PHP development,

PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; ClientContacts has a deprecated constructor in /var/www/html/crm/dal/clientcontacts.php on line 5

The Solution

(1 minute to apply in your code for a PHP developer)

The solution to avoid such a warning or error is to replace the constructor name with __construct.
It won’t affect the working of your file rather it will comply with the PHP development specifications better.

Detailed Understanding

(3 minutes read for any PHP developer)

A constructor is a special function in a class that is called automatically whenever an object of the class is instantiated.

The constructor doesn’t have a return type and it is mainly used to allocate the memory to the object with the properties you wish to use with the class object. Earlier in PHP 4, the constructors were the functions with the same name as the class. However, from PHP 5 it has been changed and you can create a constructor with __construct because constructor in PHP is a magic function. A magic method in PHP usually starts with 2 underscores.

In PHP 7 the old style of constructor having the same name of the class name is deprecated. Further, in the future version of PHP, it will be removed completely. So, at present your current project may be giving you a warning when it is running on PHP 7. But in the future, whenever you will upgrade your project to the newer version of PHP then it will start giving you an error and your project may stop working.

simple constructor example in PHP
Simple Constructor Example In PHP

Constructor Example In Old Version of PHP:

<?php  
namespace Client;  
class ClientContact {      
public function ClientContact() {          
        // treated as constructor in PHP 4 to 5.3.2          
        // treated as regular method as of PHP 5.3.3          
        // Gives warning in PHP 7.0               
}  
}  
?>

Simple Constructor Example In PHP 7.0 or higher:

<?php
namespace Client;
class ClientContact 
{
 public function __constructor() 
 {
	//put all your logic to instantiate an object with class properties           
 }
}
?>

Constructor in Parent and Child Class in PHP 7.0 or higher:

If a child class doesn’t define a constructor then the parent class constructor will be called provided it is not declared as private.

However, if you define a constructor in a child class then parent class constructor will not be called implicitly. If you wish to call the parent class constructor then you will need to call it explicitly with
parent::__construct()

<?php
class ParentClass 
{
 function __construct() 
 {
	 print "In ParentClass constructor\n";
 }
}

class ChildClass extends ParentClass 
{
 function __construct() 
 {
	 parent::__construct();
	 print "In ChildClass constructor\n";
 }
}

class OtherChildClass extends ParentClass 
{
	// inherits ParentClass's constructor
}

// In ParentClass constructor
$obj = new ParentClass();

// In ParentClass constructor
// In ChildClass constructor
$obj = new ChildClass();

// In ParentClass constructor
$obj = new OtherChildClass();
?>

Should you need any help to build or maintain your PHP development work then feel free to contact us. I and my team are always happy to help you.

Parameterized Constructor

A Constructor that accepts parameters is called a parameterized constructor. Usually, a parameterized constructor is used to initialize member variables at the time of object creation. It is similar to any other function to accept parameters.

class Client 
{
public $clientName;
public function __construct($name) 
{
     $this->clientName = $name;
}
}

$myClientObj = new Client('Jhon');
echo $myClientObj->clientName;	// Jhon

Notice that a constructor is used to create & initialize an object, therefore use $this to use/modify the class member variables.

An array as a constructor parameter

You can pass an array as a constructor parameter and auto-populate the properties.

class MyRecord 
{
    function __construct($parameters = array()) 
	{
        foreach($parameters as $key => $value) 
		{
            $this->$key = $value;
        }
    }
}

Further reading: Refer to PHP Manual Constructor and Destructors

Harshul Shah

Published by Harshul Shah

Harshul Shah is a tech geek with more than 15 years of software development experience on mobile and web technologies extensively. He is a founder CrossShore Solutions that provides custom web, mobile & enterprise application development services to clients across the globe. Harshul has acquired an in-depth understanding of mobile app designing, development, marketing, and app store optimization and helped numerous clients across the globe that includes some of the patent-pending startups, small to mid-scale businesses and fortune 500 companies.

Leave a Reply

Your email address will not be published. Required fields are marked *