代码之家  ›  专栏  ›  技术社区  ›  SilverLight

已弃用-与类同名的方法将不是将来版本的php[重复]中的构造函数

  •  -2
  • SilverLight  · 技术社区  · 6 年前

    在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行的/usr/local/ibsng/interface/ibsng/inc/errors.php中使用

    这一致命错误意味着什么?我如何修复它?

    2 回复  |  直到 6 年前
        1
  •  1
  •   ArtisticPhoenix    6 年前

    只是为了增加

    @大能领主极好的回答

    我还将重命名此函数/方法

    function Error
    

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

    在代码中还不清楚这是否是 __construct 方法与否。类的任何内部属性都不会对此方法进行修改(外部),因此每个实例可以多次调用它。但如果它是“构造器”,那么无论如何称之为 __construct()

    ps.正如@powerlord的答案所指出的,您可能还需要“名称空间”或重命名该类。

    我会避免使用 __method 键入名字,因为它对我来说很难看…大声笑

    我在PHP中切掉了那些错误…Lol.我的第一份职业是从 4.x 5.3 -那就像是2008年左右(谢谢大家的回忆php4)

        2
  •  1
  •   Powerlord    6 年前

    因为PHP7有自己的错误 Error 类,因此不能命名自己的类 误差 .