PHP 弃用:与类同名的方法在 PHP 的未来版本中将不再是构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47549783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:34:23  来源:igfitidea点击:

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

phpmysqlcodeigniter

提问by Niranjan Kumar Chowdam

Am developing application in codeigniter ,getting the below error in sending mail white registration.

正在 codeigniter 中开发应用程序,在发送邮件白注册时出现以下错误。

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

PHP 弃用:与类同名的方法在 PHP 的未来版本中将不再是构造函数;emailcomm 在第 3 行的 /var/www/html/portal/application/libraries/emailcomm.php 中有一个已弃用的构造函数

My Library file is below emailcomm.php

我的库文件在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 server to php7. before upgrading it working , Now its not working . now am checking the error logs showing above error.where can i change in this code ?? can any one please help me . Thanks

最近将服务器升级到 php7。在升级它工作之前,现在它不工作。现在正在检查显示上述错误的错误日志。我可以在哪里更改此代码??谁能帮帮我吗 。谢谢

回答by Dhairya Lakhera

In previous versions of PHP, if PHPcannot 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 DEPRECATEDin PHP 7.0, and will be removed in a future version. You should always use __construct()in new code. Read php manual

在以前的版本中PHP,如果PHP不能找到一个__construct()给定类功能,它将搜索旧式的构造函数,通过类的名字,但现在旧式的构造函数已弃用PHP 7.0,并会在将来的版本中删除. 您应该始终__construct()在新代码中使用。阅读php手册

   function __construct() {
      // copy your old constructor function code here
   }

回答by Bhupendra Mistry

You can rename your function name emailcomm() with  __construct()
use  function __construct() instead of  function emailcomm()

Or You can use following error_reporting in your config file

或者您可以在配置文件中使用以下 error_reporting

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

回答by teeyo

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

您需要使用__construct而不是与您的类同名的函数:

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.

并且只是为了编码标准,使用 CamelCase 作为以大写开头的类名。

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

还有一件事,恕我直言,您可能需要使用DotEnv库来处理您的配置,因为在代码中写下它有点混乱。

回答by vinay

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

error_reporting(E_ALL & ~E_NOTICE);- 在您的应用程序文件中删除这一行application_top.php并放入这行代码:

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

because latest php versions could not be supported deprecated files

因为最新的 php 版本不能被支持弃用的文件