Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm developing an application in CodeIgniter, and I'm getting an error in sending mail while registering.

PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; emailcomm has a deprecated constructor in /var/www/html/portal/application/libraries/emailcomm.php on line 3

My Library file is below emailcomm.php

class emailcomm
{
     
    var $to;
    var $subject;
    var $message;
    var $from='From:';    
    
    function emailcomm()
    {
        $this->CI=&get_instance();   
        ini_set("SMTP","ssl://smtp.gmail.com");
            ini_set("smtp_port","465");

            $config['protocol'] = 'smtp';
            $config['smtp_host'] = 'smtp.gmail.com';
            $config['smtp_port'] = '465';
            $config['_smtp_auth']=TRUE;
            $config['smtp_user'] = '[email protected]';
            $config['smtp_pass'] = 'Web8*98*2015'; 
            $config['smtp_timeout'] = '60';
            $config['charset'] = 'utf-8';
            $config['wordwrap'] = TRUE;
            $config['mailtype'] = "html";
        
        $this->CI->load->library('email',$config); 
        
         $this->CI->email->initialize($config); 
    }
}

Recently upgraded the server to php7, and now my code doesn't work anymore. I'm going through the error logs showing the above error. How can I fix my code?

like image 601
Niranjan Kumar Chowdam Avatar asked Nov 29 '17 09:11

Niranjan Kumar Chowdam


2 Answers

Solution : Rename your function name emailcomm() to __construct()

Explanation: In previous versions of PHP, if PHP cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class, but now old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code. Read php manual

   function __construct() {
      // copy your old constructor function code here
   }
like image 164
Dhairya Lakhera Avatar answered Nov 15 '22 19:11

Dhairya Lakhera


You can rename your emailcomm() function with __construct():

function __construct()

instead of

function emailcomm()

or you can use the following error_reporting in your config file:

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
like image 39
Bhupendra Mistry Avatar answered Nov 15 '22 18:11

Bhupendra Mistry