-2

In php v 5 these php codes have no problem :

<?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"]);
}

?>

But after installing php v7.3.2 i got this error :

Deprecated: 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

Fatal error: Cannot declare class Error, because the name is already in use in /usr/local/IBSng/interface/IBSng/inc/errors.php on line 12

What is that Fatal error mean & how can i fix it?

SilverLight
  • 19,668
  • 65
  • 192
  • 300

2 Answers2

1

Just to add to

@Powerlord Excellent answer

I would also rename this function/method

function Error

In PHP4 the constructor was named the same as the class. This had some limitations to refactoring code, copying classes etc. As you had to remember to rename them.

It's not clear in the code if this was originally intend to be the __construct method or not. None of the internal properties of the class are modified (external) to this method, so it's possible it could be called multiple times per instance. But if it is the "constructor" then by all means call it that __construct()

PS. you may want to "namespace" or rename that class too, as pointed out by @Powerlord's answer.

And I would avoid using the __method type names as it's ugly to me ... lol

I cut my teeth in PHP on those errors ... lol. First job I had professionally was migrating a site from 4.x to 5.3 - that was like circa 2008 (thanks for the memories PHP4)

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • 1
    I think you probably mean rename `function Error` to `public function __construct()` – RiggsFolly Feb 14 '19 at 00:00
  • Not if it wasn't meant to be a constructor. – ArtisticPhoenix Feb 14 '19 at 00:01
  • As I remember, that was the function of a function named the same as the class name, wasn't it? – RiggsFolly Feb 14 '19 at 00:02
  • Its a method the same name as the class `Class Error{ public function Error }` = PHP4.x constructor. But I don't see why the `Error` method could not be called repeatedly as there is no other modification of the internal data to the class outside of it. So in theory you could call `$E = new Error(); $E->Error(...)` multiple times if not for the errors. – ArtisticPhoenix Feb 14 '19 at 00:04
  • The Deprecation message being asked about - `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` – RiggsFolly Feb 14 '19 at 00:05
  • It is currently functioning as a constructor. It's also possible to call `$object->__construct()` explicitly, you know – Don't Panic Feb 14 '19 at 00:07
  • @Don'tPanic - yes I know (it is public after all), but I never really thought about that... :-p – ArtisticPhoenix Feb 14 '19 at 19:14
1

You're getting an error because PHP7 has its own Error class, so you can't name your own class Error.

Powerlord
  • 87,612
  • 17
  • 125
  • 175