【问题标题】:PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP不推荐使用 PHP:与其类同名的方法在 PHP 的未来版本中将不再是构造函数
【发布时间】:2018-05-12 23:38:21
【问题描述】:

我正在 CodeIgniter 中开发应用程序,但在注册时发送邮件时出错。

不推荐使用 PHP:与其类同名的方法将不会被 PHP 未来版本中的构造函数; emailcomm 已弃用 构造函数在 /var/www/html/portal/application/libraries/emailcomm.php 在第 3 行

我的库文件在下面 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'] = 'contact-us@webtech.com';
            $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); 
    }
}

最近将服务器升级到php7,现在我的代码不再工作了。我正在查看显示上述错误的错误日志。如何修复我的代码?

【问题讨论】:

  • 你在使用那个函数作为构造函数吗?如果是这样,将其重命名为 __construct
  • 错误信息一目了然。如需更多信息,请阅读documentation。在 PHP5 中也不推荐使用 var。改用private/protected/public 来控制从外部对对象属性的访问。
  • 请注意,deprecated 表示将来会被删除(如消息中所述...)。所以它应该仍然有效,如果你有问题,它们是由其他原因引起的。

标签: php mysql codeigniter


【解决方案1】:

解决方案:将你的函数名emailcomm()重命名为__construct()

说明:在以前的PHP 版本中,如果PHP 找不到给定类的__construct() 函数,它将按名称搜索旧式构造函数班级的, 但现在旧式构造函数在PHP 7.0已弃用,并将在未来版本中删除。您应该始终在新代码中使用 __construct()。 阅读php manual

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

【讨论】:

  • 如果emailcomm类中有多个方法怎么办?
【解决方案2】:

你可以用__construct()重命名你的emailcomm()函数:

function __construct()

而不是

function emailcomm()

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

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

【讨论】:

    【解决方案3】:

    您需要使用__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'] = 'contact-us@webtech.com';
            $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); 
    }
    }
    

    仅对于编码标准,使用 CamelCase 作为您的类名,以大写开头。

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

    【讨论】:

    • 如果我写函数 __construct() { 这样我怎么能在其中添加我的函数( emailcomm() )??
    • 它是一个构造函数,我的意思是你不需要调用那个函数,只需new Emailcomm()会自动执行构造函数中的代码(__construct
    • 我在课堂上有 4 个函数。不需要给函数 email(){ 或函数 sendmail{ 像这些???
    • 只有与类同名的函数会导致问题,其他函数都可以正常工作
    • Emailcomm 这里类名首字母必填 ??我可以像这样加载库吗? $this->load->library('Emailcomm');
    【解决方案4】:

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

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

    因为无法支持最新的 php 版本已弃用的文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      相关资源
      最近更新 更多