【问题标题】:Deprecated - Methods with the same name as their class will not be constructors in a future version of PHP [duplicate]已弃用 - 与其类同名的方法在 PHP 的未来版本中将不再是构造函数 [重复]
【发布时间】:2019-07-07 22:32:12
【问题描述】:

在 php v 5 中,这些 php 代码没有问题:

<?php

$ERRORS=array("INVALID_ERROR"=>"Invalid/Unknown error",
              "ACCESS_DENIED"=>"Access Denied",
              "INVALID_INPUT"=>"Invalid Input",
              "INCOMPLETE_REQUEST"=>"INCOMPLETE REQUEST"
            );

class Error
{ /* This Class is for errors reported from core or interface.
     Normally errors should consist of lines of ( keys and  messages), formated in a string like "key|msg"
     key shows what is error about and msg is the error message for this situation

  */
    function Error($err_str)
    {
        $this->raw_err_str=$err_str;
        $this->err_msgs=array();
        $this->err_keys=array();
        $this->__splitErrorLines();

    }

    function __splitErrorLines()
    {
        $err_lines=split("\n",$this->raw_err_str);
        foreach($err_lines as $line)
            $this->__splitError($line);
    }

    function __splitError($err_str)
    {
        $err_sp=split("\|",$err_str,2);
        if(sizeof($err_sp)==2)
        {
            $this->err_msgs[]=$err_sp[1];
            $this->err_keys[]=$err_sp[0];
        }    
        else
        {
            $this->err_msgs[]=$err_str;
            $this->err_keys[]="";
        }
    }

    function getErrorKeys()
    {/*
        Return an array of error keys
     */

        return $this->err_keys;
    }

    function getErrorMsgs()
    {/*
        Return array of error msgs
        useful for set_page_error method of smarty
     */
        return $this->err_msgs;
    }

    function getErrorMsg()
    {/* 
        Return an string of all error messages concatanated
     */
        $msgs="";
        foreach ($this->err_msgs as $msg)
            $msgs.=$msg;
        return $msgs;
    }

}

function error($error_key)
{/* return complete error message of $error_key */
    global $ERRORS;
    if (isset($ERRORS[$error_key]))
        return new Error($error_key."|".$ERRORS[$error_key]);
    else
        return new Error($ERRORS["INVALID_ERROR"]);
}

?>

但是在安装 php v7.3.2 之后我得到了这个错误:

不推荐使用:与其类同名的方法将不会被 PHP 未来版本中的构造函数;错误已弃用 /usr/local/IBSng/interface/IBSng/inc/errors.php中的构造函数上线 12

致命错误:无法声明类错误,因为名称已经 在第 12 行的 /usr/local/IBSng/interface/IBSng/inc/errors.php 中使用

致命错误是什么意思?我该如何解决?

【问题讨论】:

  • 我认为弃用是与致命错误不同的问题。

标签: php class centos7.6


【解决方案1】:

只是添加到

@Powerlord 很好的回答

我也会重命名这个函数/方法

function Error

在 PHP4 中,构造函数的名称与类相同。这对重构代码、复制类等有一些限制。因为您必须记住重命名它们。

代码中并不清楚这是否原本打算成为__construct 方法。类的所有内部属性都没有被修改(外部)为此方法,因此每个实例可能会多次调用它。但如果它是“构造函数”,那么无论如何都称它为__construct()

附言。正如@Powerlord 的回答所指出的那样,您可能还想“命名空间”或重命名该类。

而且我会避免使用 __method 类型名称,因为它对我来说很难看……哈哈

我在 PHP 中遇到了这些错误……哈哈。我的第一份专业工作是将网站从 4.x 迁移到 5.3 - 大约是 2008 年(感谢 PHP4 的记忆)

【讨论】:

  • 我想你的意思可能是把function Error重命名为public function __construct()
  • 如果它不是构造函数,则不会。
  • 我记得,那是一个与类名同名的函数,不是吗?
  • 它是一个与类Class Error{ public function Error } = PHP4.x 构造函数同名的方法。但我不明白为什么不能重复调用Error 方法,因为没有其他内部数据修改到它之外的类。所以理论上你可以多次调用$E = new Error(); $E-&gt;Error(...),如果不是因为错误。
  • 被问及的弃用消息 - Methods with the same name as their class will not be constructors in a future version of PHP; Error has a deprecated constructor in /usr/local/IBSng/interface/IBSng/inc/errors.php on line 12
【解决方案2】:

您收到错误是因为 PHP7 有自己的 Error 类,因此您不能将自己的类命名为 Error

【讨论】:

  • 这实际上是他们将得到的下一个错误。
  • @ArtisticPhoenix 他们询问致命错误是什么。正如前面提到的 Don't Panic,Warning 与 Fatal 错误无关。
  • 好的,我知道了...我刚读了第一个。
猜你喜欢
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多