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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

When upgrading from PHP 5 to PHP 7, the language introduced stricter rules regarding object initialization that often break legacy codebases written for older standards. You are likely seeing this specific deprecation warning because your emailcomm class defines a method named emailcomm, which was previously treated as an implicit constructor in PHP 5. In modern PHP versions, the language expects you to explicitly define a method named __construct() if you want initialization logic to run when a new instance is created.

The error message specifically points to /var/www/html/portal/application/libraries/emailcomm.php on line 3. The core issue here is that CodeIgniter (and many other frameworks) rely on the framework's own constructor methods to handle service initialization. When you define a method with the same name as your class, PHP 5 silently assumed it was a constructor. However, PHP 7 and later versions deprecated this behavior to enforce explicit naming conventions, ensuring that developers are clear about their intent.

Fixing the Constructor Pattern

To resolve this warning, you must rename the existing emailcomm() method to __construct(). This change aligns your code with current PHP standards and ensures backward compatibility with future releases. Here is how the corrected class structure should look:

class emailcomm {
    
    public $to;
    public $subject;
    public $message;
    // It is best practice to use type hints where possible, though var was common in legacy code
    
    /**
     * The constructor for the EmailComm class.
     */
    public function __construct() {
        // Access CodeIgniter's super object
        $this->CI = get_instance();   
        
        // Configure SMTP settings directly within the constructor
        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; // Note: Port should ideally be an integer in newer configs
        $config['_smtp_auth'] = TRUE;
        
        // Security Warning: Never commit hardcoded credentials to version control
        // Replace these with environment variables or a config file in production
        $config['smtp_user'] = 'contact-us@webtech.com';
        $config['smtp_pass'] = 'Web8*98*2015'; 
        $config['smtp_timeout'] = 60;
        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        
        // Load the library with the configuration array
        $this->CI->load->library('email', $config); 
        
        // Initialize the email library instance
        $this->CI->email->initialize($config); 
    }
}

Why This Change Matters

The primary reason for this strictness in PHP 7 is to prevent ambiguity. In PHP 5, relying on method name matching was a convenience feature that often led to unintended consequences when class names changed or were reused. By requiring __construct(), the language forces developers to explicitly declare initialization logic.

Furthermore, there are significant architectural trade-offs in how you are handling configuration in this specific example. You are currently setting ini_set directives inside the constructor. While this works for legacy setups, relying on global PHP settings like ini_set('SMTP', ...) is generally discouraged in modern applications because it can interfere with other parts of your application or cause issues if multiple instances of the class attempt to run in different contexts.

Additionally, you are hardcoding sensitive information directly inside the constructor. The SMTP password and email address are visible in the source code. If this repository is ever exposed or pushed to a public index, these credentials become compromised immediately. A more robust approach would be to load configuration from an environment-specific file (like .env) or pass a configuration array into the library via a dependency injection container, rather than baking it into the class definition.

CodeIgniter Context

If you are maintaining a legacy CodeIgniter 3 application, you might encounter this issue frequently because the framework itself often uses implicit constructors in its libraries. However, you should not rely on deprecated behaviors for your own custom libraries or controllers. Updating emailcomm.php to use __construct() is the minimal change required to silence the warning and future-proof the code.

While upgrading PHP versions brings security benefits and performance improvements, it inevitably breaks legacy patterns that worked silently in the past. Addressing these deprecation warnings early prevents them from becoming fatal errors in subsequent minor version updates. If you are transitioning to a newer framework like Laravel, you will find that these explicit constructor requirements are enforced even more strictly by default.

For further reading on configuring email services securely within modern PHP applications, consult resources like https://laravelcompany.com, which provides detailed guides on handling SMTP authentication and environment configuration without exposing secrets in your codebase.

Our network