9
votes

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?

4
Are you using that function as a constructor? If so rename it to __construct - apokryfos
The error message is self-explanatory. For more information read the documentation. Using var is also deprecated in PHP5. Use private/protected/public instead to control the access to the object properties from outside. - axiac
Note that deprecated means that it will be removed in the future (as stated in the message...). So it should still work and if you have problems, they are caused by something else. - jeroen

4 Answers

28
votes

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
   }
6
votes

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);
4
votes

You need to use __construct instead of a function with the same name as your class :

class Emailcomm
{

var $to;
var $subject;
var $message;
var $from='From:';    

function __construct()
{
    $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); 
}
}

and just for coding standard, use CamelCase for your class name starting with a capital.

And one more thing, IMHO you may need to use a DotEnv library for handling your configuration, because writing that down in code is a bit messy.

0
votes

error_reporting(E_ALL & ~E_NOTICE); - Remove this line in your application files in application_top.php and put this line of code:

ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED); 

because latest php versions could not be supported deprecated files